I'm trying to create a form with Simple Form to edit users profile from admin interface.
Here are my routes :
namespace :admin do
resources :users
root 'admin#index'
end
Admin::UsersController :
def edit
#user = User.find(params[:id])
end
def update
#user = User.find(params[:id])
if #user.update(user_params)
redirect_to admin_user_path(#user.id)
else
render :edit
end
end
And my view :
<div class='container mt-4'
<%= simple_form_for [:admin, #user] do |f| %>
<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
<%= f.input :first_name, label: 'Prénom' %>
<%= f.input :last_name, label: 'Nom' %>
<%= f.input :description %>
<%= f.input :email %>
<%= f.button :submit, 'Enregistrer les modifications', class: 'btn btn-primary rounded' %>
<% end %>
</div>
But my form doesn't work, nothing happen when I click the button... What did I miss ?
I think that the problem is in the route.
namespace :admin do
resources :users
root 'admin#index'
get 'admin#update'
end
Related
I am having issues getting my create method to function correctly.
Here is my new.html.erb file
<div class="col-md-8">
<%= form_for #wiki do |f| %>
<%= f.label :title, class: 'form-control' %>
<%= f.text_field :title, class: 'form-control', placeholder: "Enter wiki title" %><br><br>
<%= f.label :body, class: 'form-control'%>
<%= f.text_area :body, class: 'form-control', placeholder: "Enter wiki body" %><br><br>
<%= f.check_box :private %> Private Topic<br><br>
<%= f.submit "Save", class: 'btn btn-success' %>
<% end %>
</div>
Controller
def new
#wiki = Wiki.new
end
def create
#wiki = Wiki.create(params[:wiki])
if #wiki.save
flash[:notice] = "Wiki has been saved"
redirect_to wiki_index_path
end
end
private
def wiki_params
params.require(:wiki).permit(:title, :body, :private)
end
routes.rb
Rails.application.routes.draw do
devise_for :users
resources :wiki
post 'wiki/new'
post 'wiki/create'
resources :users
root 'welcome#index'
end
If I use :wiki in my form_for on new.html.erb I can get passed the error but when clicking on my save button on the actual new page, nothing happens at all (since I'm not saving the information to the correct variable). However, as soon as I use the #wiki variable, I get the error
"undefined method `wikis_path' for #<#<Class:0x007f8f794e5d58>:0x007f8f7bb2ef50>"
Any help is appreciated!
You can get rid of redundant unrestful wiki routes. resources :wikis will be enough:
Rails.application.routes.draw do
devise_for :users
resources :wikis
resources :users
root 'welcome#index'
end
And something tells me that all will be fine after that change.
I want to create a form for "Comments" route which is a member of Article Resources:
resources :articles do
member do
post 'comments'
end
end
I want the comment form to be in Articles#Show page. The problem i got an error:
First argument in form cannot contain nil or be empty
If the for is like this:
<div>
<%= form_for #comm do |c| %>
<%= c.label :Your_comment %>
<%= c.text_area :commBody %>
<%= c.submit 'submit' %>
<% end %>
</div>
So how to do it ?
If this is your controller,
def show
#article = Article.find(params[:id])
end
and you want to create a form for a new Comment related to #article that points to POST /articles/3/comments:
<%= form_for([#article, Comment.new], as: :article, url: comments_article_path(#article)) do |f| %>
<%= f.label :body %>
<%= f.text_area :body %>
<%= f.submit 'Submit' %>
<% end %>
Don't forget to add accepts_nested_attributes_for :comments in the Article model. And also don't forget to setup the whitelisted params in the ArticleController.
Another thing: don't use abbreviations for your variable names. Use #article and #comment, not #art and #comm.
#config/routes.rb
resources :articles do
post :comment, on: :member #-> url.com/articles/:id/comment
end
#app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
def show
#article = Article.find params[:id]
#comment = #article.comments.new
end
end
#app/views/articles/show.html.erb
Comment:
<%= form_for [#article, #comment], url: article_comment_path(#article) do |c| %>
<%= c.label "Your Comment" %>
<%= c.text_area :commBody %>
<%= c.submit %>
<% end %>
For some reason the edit action won't render i get this error and is using show action instead of edit but the same form works for the render :new action
do not focus on the params[:preview], i am talking about the last render :edit
ActionController::UrlGenerationError in Admin::Blog::Posts#update
No route matches {:action=>"show", :controller=>"admin/blog/posts", :id=>""} missing required keys: [:id]
def edit
#post = Post.find_by_permalink(params[:id])
end
def update
#post = Post.find_by_permalink(params[:id])
if params[:publish]
#post.publish
elsif params[:draft]
#post.draft
end
if params[:preview]
if #post.published?
#post.draft
end
if #post.update(blog_post_params)
flash[:success] = "some text "
redirect_to blog_post_url(#post)
else
render :edit
end
end
if #post.update(blog_post_params)
flash[:success] = "Post was successfully updated."
redirect_to edit_admin_blog_post_url(#post)
else
render :edit
end
end
form
<%= form_for [:admin,:blog, #post] do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="large-12 columns">
<div class="field panel">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field panel">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<div class="actions panel text-right">
<% if #post.published? %>
<%= f.submit "Save Changes",name: "publish", class: "tiny button radius success" %>
<% else %>
<%= f.submit "Publish",name: "publish", class: "tiny button radius success" %>
<% end %>
<%= f.submit 'Mark as Draft', name: "draft", class: "tiny button radius " %>
<% if #post.created_at %>
<%= f.submit 'Preview', name: "preview", class: "tiny button radius warning" %>
<% end %>
<%= link_to 'Back', admin_blog_posts_path, class: "tiny button radius secondary" %>
</div>
<div class="field panel">
<%= f.label :body %><br>
<%= f.cktext_area :body, :height => '800px', :id => 'sometext' %>
</div>
</div>
<% end %>
relevant routes
namespace :admin do
namespace :blog do
get '', to: 'welcome#index', as: '/'
resources :posts
end
end
post model
class Post < ActiveRecord::Base
has_many :tags
has_many :comments
before_validation :generate_permalink
validates :permalink, uniqueness: true
validates :title, presence: true
validates :description, presence: true
def generate_permalink
self.permalink = title.parameterize
end
def to_param
permalink
end
end
I guess i know why you get this error.
In the edit action you use Post.find_by_permalink(params[:id]) to find your post, which returns nil if nothing was found. And since you may change the title attribute, your permalink is updated (i guess), and your post is not found. The controller still renders the action, with nil #post, and cannot generate the url for the form.
Try using Post.find_by_permalink!(params[:id]) instead, and you will get a 404.
I would actually suggest you to use regular find in the admin area, since the permalink might change.
Hi I have a working "blog module" on my page.
From the start. My routes looks like this:
root 'static_pages#home'
resources :announcements, only: [:new,:create,:update,:edit,:destroy,:show] do
resources :comments, only: [:create, :destroy]
end
My controller actions looks like this:
static_pages_controllers.rb
def home
#announcements = Announcement.page(params[:page]).order('id DESC')
end
announcements_controller.rb
def new
#announcement = Announcement.new
end
def create
#announcement = Announcement.new(announcement_params)
if #announcement.save
flash[:success] = "Post został pomyślnie dodany"
redirect_to root_path
else
render 'new'
end
end
comments_controller.rb
def create
#announcement = Announcement.find(params[:announcement_id])
#comment = #announcement.comments.new(comments_params)
if #comment.save
flash[:success] = "Komentarz dodano"
redirect_to root_path }
else
render 'staticpages#home'
end
end
Part of the home.html.erb which lists all anouncements and under each of them it adds a form to add a comment:
<% #announcements.each do |announcement| %>
.... some code ....
<% if current_user && user_signed_in? %>
<div class="create_comment" >
<%= render partial: 'shared/create_comments_form', locals: {announcement: announcement } %>
</div>
<% end %>
... some code ...
<% end %>
And _create_comment.html.erb :
<%= form_for announcement.comments.create, url: announcement_comments_path(announcement.id) do |form| %>
<%= form.label :author, "#{current_user.username}:" %>
<%= form.hidden_field :author, value: current_user.username %>
<%= form.hidden_field :user_id, value: current_user.id %>
<%= form.text_field :content, class: "form-control content_length" %>
<div class="char_counter">255</div>
<%= form.submit "Dodaj!", class: "btn btn-primary" %>
<% end %>
My question is there a better way of writing this form, because It just doesn`t look rails for me when I have to specify everywhere all those parameters. Eg. when I send parameters to _create_comment I have to pass announcement object, and in form it self I have to specify the url as it looks for create_comment_path by default.
I think you wanted just to handle nested resources, and need a form to create such a nested resource.
Then this should work:
form_for( [announcement, announcement.comments.new] ) do |form|
Do not use create in the form template, as it actually saves it to the database, you end with lots of empty comments just because somebody viewed the form.
You can use cokoon gem for this. This gem will create Dynamic nested forms using jQuery.
Also I have one more suggestion. Rather then using only you can use except. Because you have used all 6 routes without index.
root 'static_pages#home'
resources :announcements, except: [:index] do
resources :comments, only: [:create, :destroy]
end
Update:
You can use like below:
Form:
<%= form_for :comments do |form| %>
<%= form.label :author, "#{current_user.username}:" %>
<%= form.hidden_field :author, value: current_user.username %>
<%= form.hidden_field :user_id, value: current_user.id %>
<%= form.text_field :content, class: "form-control content_length" %>
<div class="char_counter">255</div>
<%= form.submit "Dodaj!", class: "btn btn-primary" %>
<% end %>
For more info: http://www.sitepoint.com/complex-rails-forms-with-nested-attributes/
I got error from browser after sign up with email and password.
class UserStepsController < ApplicationController
steps :personal
def show
#user = current_user
render_wizard
end
def update
#user = current_user
#user.update_attributes(user_params)
render_wizard #user
end
def finish_wizard_path
flash[:notice] = "You have successfully Registered With The Wizard."
user_path(current_user)
end
private
def user_params
params.require(:user).permit(:first_name, :last_name, :degree_level, :role, :email, :password )
end
end
On RegistrationsController < Devise::RegistrationsController
class RegistrationsController < Devise::RegistrationsController
def new
super
end
def create
super
end
protected
def after_sign_up_path_for(resource)
'/user_steps'
end
end
I allready added routes resources :user_steps, devise_for :users, :controllers => { :registrations => "registrations" }
my devise/registrations/new
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :email, required: true, autofocus: true %>
<%= f.input :password, required: true %>
<%= f.input :password_confirmation, required: true %>
</div>
<div class="form-actions">
<%= f.button :submit, "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
Added personal.html.erb first name, last name and degree_level
<%= form_for(#user, :url => wizard_path, :method => :put) do |f|%>
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<%= f.text_field :degree_level %>
<%= f.submit 'submit', :class => "btn btn-primary" %>
<%end%>
user model added validates presence.
This error show after sign up with email and password
error message is Please review the problems below:
Sorry i want add include Wicked::Wizard