Ruby on rails RoR 3:设计,拆分编辑用户表单以分离更改密码,设计错误消息没有方法错误!

Ruby on rails RoR 3:设计,拆分编辑用户表单以分离更改密码,设计错误消息没有方法错误!,ruby-on-rails,ruby-on-rails-3,forms,devise,Ruby On Rails,Ruby On Rails 3,Forms,Devise,我试图将更改密码字段拉到一个单独的表单中,但方法designe\u error\u message!出错 如果我删除了更新工作的方法,但是,如果验证失败,它将重定向到registrations/edit.html.erb并给出错误消息。我如何让它重定向回registrations/change_password.html.erb视图并给出designe_error_消息 我所有的代码: 我认为有几件事需要解决 1)在更改密码操作中初始化资源 调用设计错误消息失败,因为您没有在Registrati

我试图将更改密码字段拉到一个单独的表单中,但方法designe\u error\u message!出错

如果我删除了更新工作的方法,但是,如果验证失败,它将重定向到registrations/edit.html.erb并给出错误消息。我如何让它重定向回registrations/change_password.html.erb视图并给出designe_error_消息

我所有的代码:
我认为有几件事需要解决

1)在更改密码操作中初始化资源

调用
设计错误消息失败,因为您没有在
RegistrationsController#change_password
操作中初始化
资源
(您的用户模型实例)。一种方法是确保
验证\u范围before过滤器在
change\u password
操作中被调用。在您的注册控制器中尝试类似的操作

class RegistrationsController < Devise::RegistrationsController
    prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy, :change_password]

    def create
      ...
    end
end
无论哪种方式,您都需要覆盖designe::RegistrationsController#udpate操作。大概是这样的:

class RegistrationsController < Devise::RegistrationsController
    prepend_before_filter :authenticate_scope!, :only => [: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 => true
        respond_with resource, :location => 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
类注册控制器[:编辑、更新、销毁、更改密码]
def更新
if resource.update_与_密码(参数[resource_name])
设置\u flash\u消息:注意,:如果是导航格式,则更新?
在资源中签名\u名称,资源::旁路=>true
使用资源响应:位置=>在更新(资源)的路径之后
其他的
清除密码(资源)
用导航(资源)do响应
如果参数[:更改密码]#或闪存[:更改密码]
使用\u作用域呈现\u:更改\u密码
其他的
使用\u范围渲染\u:编辑
结束
结束
结束
结束
结束

尝试一下,但我认为这会让你回到正轨。

嗨,谢谢你的帮助!这似乎非常接近。我没有得到任何方法错误的回应与_导航,任何想法这不是继承?我甚至尝试在RegistrationController中添加“include designe::Controllers::Helpers”,但没有成功。自定义RegistrationController中应该可以直接访问
clean\u passwords()
respond\u with\u navigational()
方法。不需要包含任何模块。是否有堆栈跟踪或完整的错误消息?不确定发生了什么。尝试将第22行到第27行还原为以前的
respond_with_navigational(resource){render_with_scope:edit}
,看看问题是否是由块引起的。您是对的,它是方法,错误仍然存在,当密码不匹配时,没有方法错误respond_with_navigational,导航格式是否没有方法错误?当他们匹配的时候。为什么这些方法不能继承?我不确定。我将通过删除RegistrationController中的所有自定义项来实现这一点,然后逐步添加自定义代码,看看是什么导致了这些失败。我怀疑这可能与
prepend\u-before\u-filter
有关,所以现在试着对它进行注释,看看是否还能正常工作。如果
prepend\u before\u filter
证明是罪魁祸首,则尝试直接在
change\u password
操作中创建
资源
(而不是使用before filter),即在
change\u password
方法的开头添加
构建资源
class RegistrationsController < Devise::RegistrationsController
    prepend_before_filter :authenticate_scope!, :only => [: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 => true
        respond_with resource, :location => 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