Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I separate out the devise form like gorails.com (edit profile, edit password)?

Lance Williams asked in General

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!

Reply

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]
Reply

Yup, that's basically how I did it too. :)

I just created two form_fors 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. :)

Reply
Hi Chris, is there a way we could separate this into two pages, with a page to update password and a page to update profile ? i am also new to Rails and have been trying for the last 2 days. It seems easy but for some reason I am getting a lot of errors
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,563+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.