How do I separate out the devise form like gorails.com (edit profile, edit password)?
I've followed this link https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-password, but would like to just use the update action and not have to use update_password & update_profile custom actions.
Chris, on gorails.com on the settings page you have 'edit profile' and 'edit password' going to the default 'update action'. What kind of logic did you use to allow multiple forms submitting to the /users (update) action.
Thanks so much!
update: I did it the hard way. Chris already has a video on this: https://gorails.com/episodes/forms-with-multiple-submit-buttons?autoplay=1
I think I figured it out. I don't know if it's the best way but it seems to be working. I'm brand new to rails so I'm learning as I go. Thanks for the awesome content Chris.
In the UsersController in the update action I added:
if params[:commit] == "Update Profile"
# logic for update profile form
end
if params[:commit] == "Update Password"
# logic for update password form
end
In the edit.html.erb file the form submit button value name matched the params[:commit] comparison like this:
<%= form_for(@user, url: {action: "update"}, html: {id: "profile"}) do |f| %>
# profile fields go here
<%= f.submit "Update Profile" %>
<% end %>
<%= form_for(@user, url: {action: "update"}, html: {id: "password"}) do |f| %>
# password fields go here
<%= f.submit "Update Password" %>
<% end %>
in routes.rb I put this:
resource :user, only: [:edit, :update]
Yup, that's basically how I did it too. :)
I just created two form_for
s and separated them out. Since both of mine run through Devise, you don't have to do any of the updates to the action because it will just save whatever fields you pass in and it doesn't matter which button was submitted because the logic doesn't change.
You might be able to refactor your action so that it handles things the same way. Basically always try to update the name and profile information. If you didn't receive and field data for it, then no updates are applied. Same for the password. If you didn't pass in the field, it won't attempt to update the password. That would help you remove those two if statements. :)