Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's a few things that I think need to be resolved.</p> <p><strong>1) Initialize the resource in the change_password action</strong></p> <p>The call to <code>devise_error_messages!</code> fails because you aren't initializing the <code>resource</code> (your User model instance) in the <code>RegistrationsController#change_password</code> action. One way to do this is to make sure that the <code>authenticate_scope!</code> before filter, which is implemented in Devise::RegistrationsController, gets called on the <code>change_password</code> action. Try something like this in your RegistrationsController.</p> <pre><code>class RegistrationsController &lt; Devise::RegistrationsController prepend_before_filter :authenticate_scope!, :only =&gt; [:edit, :update, :destroy, :change_password] def create ... end end </code></pre> <p>If that doesn't work, you might want to simply call <code>authenticate_scope!</code> at the beginning of your <code>change_password</code> action.</p> <p><strong>2) Redirect to change_password.html.erb in case of a failure</strong></p> <p>Basically both of the <code>Devise::RegistrationsController#edit</code> action and your <code>RegistrationsController#change_password</code> action submit a form to the <code>Devise::RegistrationsController#update</code> action. What you want to do is make sure that when an update fails, if the form submission is coming from the <code>Devise::RegistrationsController#edit</code> action then you render the <code>registrations/edit.html.erb</code> view and similarly if the form submission is coming from the <code>RegistrationsController#change_password</code> action then you render <code>registrations/change_password.html.erb</code> view.</p> <p>There are various ways to do this including relying on the flash hash to set a key in the <code>RegistrationsController#change_password</code> action (e.g. <code>flash[:change_password] = true</code>) and then check for the presence of this key if an error occurs during an update. Another approach would be to use a hidden field in your change_password form then similarly, if an error occurs during an update, check for the presence of this hidden field in the params hash. Something like this.</p> <pre><code>&lt;h2&gt;Edit &lt;%= resource_name.to_s.humanize %&gt;&lt;/h2&gt; &lt;%= form_for(resource, :as =&gt; resource_name, :url =&gt; registration_path(resource_name), :html =&gt; { :method =&gt; :put }) do |f| %&gt; &lt;%= devise_error_messages! %&gt; &lt;%= hidden_field_tag :change_password, true %&gt; </code></pre> <p>Either way you will need to override the Devise::RegistrationsController#udpate action. Something like this:</p> <pre><code>class RegistrationsController &lt; Devise::RegistrationsController prepend_before_filter :authenticate_scope!, :only =&gt; [:edit, :update, :destroy, :change_password] def update if resource.update_with_password(params[resource_name]) set_flash_message :notice, :updated if is_navigational_format? sign_in resource_name, resource, :bypass =&gt; true respond_with resource, :location =&gt; after_update_path_for(resource) else clean_up_passwords(resource) respond_with_navigational(resource) do if params[:change_password] # or flash[:change_password] render_with_scope :change_password else render_with_scope :edit end end end end end </code></pre> <p>Give this a try but I think that should put you back on track.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload