User CRUD in web application that handles registration and login with Devise
I am currently working on application that is build on rails 3.0.9. All of a sudden the client decided that there should be a place to create a user and edit a user in the admin section of the website.
Now here is the case. The authentication and registration in the web application is handled by devise. If I try to implement a custom USER create method in my controller how should I hash the password in the very same way devise is doing so that I can store that in the database. This also applies to editing the already registered users as well.
I have tried开发者_JS百科 to find the solution but no use. Any help in resolving this would be appreciated.
That's easy. You can setup another controller and form but set it up on your User model. Your form will need to include :email
, :password
, and :password_confirmation
. When you do @user.save
in your controller's create action, this will have devise take care of all the hashing requirements under the hood.
If you want to check that your save works (just for testing), add a bang at the end like @user.save!
- this is only for testing. Either drop into rails console and you can see the newly added records or tail your log file.
Editing should work along the same lines and you can do @user.update_attribute()
in your edit action, or @user.update_attributes()
if you prefer mass assignment (this will be subject to any attr_accessible
restrictions in your model)
精彩评论