html - How to use the same form for creating and editing in ruby on rails -
i new ruby. have same form need perform creation/updation , when required. problem have whenever call edit instead of editing details of existing user, getting new user edited details. put simply, think whenever perform edit, create method being called.
so there way use single form both new , edit instead of using separate forms.
the following code editing user details:
<%= link_to 'home', root_path %> <h2>edit user</h2> <%= form_for :user, url: user_index_path |f| %> <p> <%= f.label :name %><br> <%= f.text_field :name %> </p> <p> <%= f.label :address %><br> <%= f.text_area :address %> </p> <p> <%= f.label :email %><br> <%= f.text_field :email %> </p> <p> <%= f.label :phone %><br> <%= f.text_field :phone %> </p> <p> <%= f.label :state %><br> <%= f.text_field :state %> </p> <p> <%= f.label :country %> <%= f.collection_select(:country, country.all, :name, :name) %> </p> <p> <%= f.submit %> </p> <% end %>
the following code creating user:
<%= link_to 'home', root_path %> <h2>create new user</h2> <%= form_for :user, url: user_index_path |f| %> <p> <%= f.label :name %><br> <%= f.text_field :name %> </p> <p> <%= f.label :address %><br> <%= f.text_area :address %> </p> <p> <%= f.label :email %><br> <%= f.text_field :email %> </p> <p> <%= f.label :phone %><br> <%= f.text_field :phone %> </p> <p> <%= f.label :state %><br> <%= f.text_field :state %> </p> <p> <%= f.label :country %> <%= f.collection_select(:country, country.all, :name, :name) %> </p> <p> <%= f.submit %> </p> <% end %>
and controller:
class usercontroller < applicationcontroller def index @users = user.all end def show @user = user.find(params[:id]) end def new @user = user.new end def create @user = user.new(user_params) if @user.save redirect_to @user else render 'new' end end def edit @user = user.find(params[:id]) end def update #@user = user.find(params[:id]) if @user.update(user_params) redirect_to @user else render 'edit' end end def destroy @user = user.find(params[:id]) @user.destroy redirect_to user_index_path end private def user_params params.require(:user).permit(:name, :address, :email, :phone, :state, :country) end end
since new ruby, did not exact details of how http requests function in ruby.
users/_form.html.erb
<%= link_to 'home', root_path %> <%= form_for @user |f| %> <p> <%= f.label :name %><br> <%= f.text_field :name %> </p> <p> <%= f.label :address %><br> <%= f.text_area :address %> </p> <p> <%= f.label :email %><br> <%= f.text_field :email %> </p> <p> <%= f.label :phone %><br> <%= f.text_field :phone %> </p> <p> <%= f.label :state %><br> <%= f.text_field :state %> </p> <p> <%= f.label :country %> <%= f.collection_select(:country, country.all, :name, :name) %> </p> <p> <%= f.submit %> </p> <% end %>
users/new.html.erb
<h2>create new user</h2> <%= render 'form' %>
users/edit.html.erb
<h2>edit user</h2> <%= render 'form' %>
users_controller.rb
class userscontroller < applicationcontroller before_filter :find_user, only: [:show, :edit, :update, :destroy] def index @users = user.all end def show end def new @user = user.new end def create @user = user.new(user_params) if @user.save redirect_to @user else render 'new' end end def edit end def update if @user.update(user_params) redirect_to @user else render 'edit' end end def destroy @user.destroy redirect_to user_index_path end private def user_params params.require(:user).permit(:name, :address, :email, :phone, :state, :country) end def find_user @user = user.find(params[:id]) end end
when have same view code 2 or more methods, should create partial of same view code , render in methods. called dry principle in ruby on rails
Comments
Post a Comment