Rendering the Devise edit Password Form

2019-02-10 08:22发布

问题:

I'm trying to render the Devise edit password form within another view because I don't want to duplicate the edit pw logic.

I've tried the following (after generating the Devise views):

<%= render 'devise/passwords/edit' %>
<%= render 'devise/passwords/form' %>

And a number of other variations on render that all seem to give me the same error:

"ActionView::MissingTemplate in foo#foo Missing partial devise/passwords/edit..."

This variation:

  <%= render :file => 'devise/passwords/edit.html.erb' %>

Gave me some hope but the following error:

"undefined local variable or method `resource' for #<#:0x47ef0e0>"

around this line:

<%= form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :put }) do |f| %>

That makes me think I'm close (as that is code from the form that I want) but shouldn't that template be using the correct logic from the hidden Devise controller? Or do I need to do something in the routes file to get this to work?

Am I way off?

回答1:

Try this:

<%= render :template => 'devise/passwords/edit', 
                        :locals => { 
                          :resource => my_user_model_variable, 
                          :resource_name => my_user_model_name } %>

Where:

  • my_user_model_variable could be current_user
  • my_user_model_name could be "User"