I am trying to save each field of a form individually. But I have really hard time implementing it.
Here's the code I am trying.
users_controller.rb:
def update_name
@user = User.find(params[:id])
if @user.update_attributes(params.require(:user).permit(:name))
# Handle a successful update.
flash[:success] = "Name updated"
redirect_to edit_user_path
else
render 'edit'
end
end
def update_email
@user = User.find(params[:id])
if @user.update_attributes(params.require(:user).permit(:email))
# Handle a successful update.
flash[:success] = "Email updated"
render 'edit'
else
render 'edit'
end
end
routes.rb:
get 'users/:id/edit' => 'users#edit', :as => :edit_user
get '/users/:id' => 'users#show', :as => :user
patch 'users/:id/name'=> 'users#update_name'
patch 'users/:id/email'=> 'users#update_email'
edit.html.erb:
<%= f.button "Save", :action => "update_name" , :method => :patch, class: "btn btn-primary", id:"editAccount" %>
<%= f.button "Save", :action => "update_email" , :method => :patch, class: "btn btn-primary", id:"editAccount" %>
I can go with normal update but I would like to save the fields individually.
How can I fix this? Thanks
Aucun commentaire:
Enregistrer un commentaire