开发者

Devise edit password page - access user model

开发者 https://www.devze.com 2023-03-07 13:00 出处:网络
I need to customise the Devise edit password page to include a few details that are included in the User model.

I need to customise the Devise edit password page to include a few details that are included in the User model.

I had a quick look online but couldn't find any documentation mentioning the views having access to the user model.

Is there a way to access it?

Edit: I've got the views et al, it's specifically about accessing the user model in the edit pass开发者_Python百科word page. I need to personalise it.


= render :template => 'devise/passwords/edit', :locals => {:resource => current_user,:resource_name => User }

Try this in view For devise_errors include devise_helper.rb or write your own error handler


Run this and it will copy your views to a folder called 'shared' for R<3 and 'devise' for R3.

rails g devise:views

You can then customize the views. You should be able to understand everything in those volders it's just Rails MVC stuff.

If you have a specific devise model you should specific the model by name:

rails g devise:views users


rails generate devise:views users

This will generate the views for the user model. You can then manually edit the ones you want.


Check out the devise Wiki on github ( https://github.com/plataformatec/devise/wiki ), furthermore what you prolly want to do is run the following command in your app home. rails g devise:views or for a specific model rails g devise:views modelName

that will create the tree of views under app/views/devise.

In the views, refer to the object as resource

<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
...

You can safely delete the views that you don't need to over ride, if you want to use haml or slim for views, that is fine too, see this wiki page => https://github.com/plataformatec/devise/wiki/How-To:-Create-Haml-and-Slim-Views


I wanted to do the same thing. I temporarily put resource.inspect into the view, and saw that it was a User instance with all attributes nil except for reset_password_token. Given that, I accessed the corresponding User record using brute force:

User.find_by_reset_password_token(resource.reset_password_token)

Or to be more generic:

resource.class.find_by_reset_password_token(resource.reset_password_token)

You can use that directly in view, or assign it to an instance variable in the controller if you're overriding the Devise controller. Be aware that it will return nil if the token is invalid. (It seems Devise doesn't check the validity of the token until after the user submits the form, so you can get to this view with an invalid token.)

Update 03/2014

As of devise 3.1, you need to digest the token before lookup:

resource.class.find_by_reset_password_token(Devise.token_generator.digest(resource.class, :reset_password_token, resource.reset_password_token))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号