Rails - Devise - edit_user_registration_path
with Rails there i开发者_如何转开发s the path, edit_user_registration_path to allow users to edit their profile.
I'm setting up my own Account Control to present the user with a better UI to edit their profile etc.. which is routed at /account/profile, /account/notices, etc....
Problem is this URL, /users/edit still works and takes users to the DEVISE edit page.
How can I have that always go to the new edit page /account/profile
Thanks
This is probably the worst part about devise is making a custom edit profile path. The reason for this is when you try to update your resource, it's going to send you back to the default path for editing the user, that is if you get errors.
What I would suggest is that you keep the default users/edit path and then edit assocations, not the actual resources. Otherwise you are going to have to dig into the gem and rewrite the paths of how users are edited.
This is what I did.
In your User model user.rb
has_one :profile
has_many :notices
Then you can have a notices
and profiles
controller where you edit those and not the user or the resource
which is what you made with the devise helpers and will make it harder to customize. Make a hidden_field f.hidden_field :user_id, :value => current_user.id
for those forms and it will save the user when you create them and update them etc...
As of the latest Devise release (specifically this commit), you can now specify a custom path for the edit profile action via path_names
:
devise_for :users, path_names: { edit: 'profile' }
A simpler way is just to create a ProfilesController, and in your routes, just define "resource :profile". Then you just define the "edit" and "update" methods and you're done.
I haven't taken it much further than the basics, but I really don't see the downside, and it's really simple in comparison to anything that's been offered here.
Solution:
def update
self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)
prev_unconfirmed_email = resource.unconfirmed_email if resource.respond_to?(:unconfirmed_email)
if update_resource(resource, account_update_params)
yield resource if block_given?
if is_flashing_format?
flash_key = update_needs_confirmation?(resource, prev_unconfirmed_email) ?
:update_needs_confirmation : :updated
set_flash_message :notice, flash_key
end
sign_in resource_name, resource, :bypass => true
respond_with resource, :location => after_update_path_for(resource)
else
clean_up_passwords resource
#Add a conditional redirect depending on where the controller was called from
if URI(request.referer).path == '/users/edit'
respond_with resource
else
redirect_to after_update_path_for(resource), :flash => { :alert => "Please enter your password" }
end
end
end
精彩评论