覆盖设计注册控制器 [英] Override devise registrations controller
问题描述
我已经添加了一个基于不同模型的注册表单的字段,请参见如何使用嵌套属性与设计模型进行详细说明。这个部分工作正常。
现在的问题是,当我保存时,由devise提供的注册控制器的创建操作失败,一个 Activerecord :: UnknownAttributeError
在此字段(公司)。
我假设我需要覆盖注册控制器,或者是在那里一个更好/更容易的方式我应该接近这个?
在您的表格中,您通过大量分配传递任何其他属性不属于您的用户模型或任何嵌套模型?
如果是这样,我相信ActiveRecord :: UnknownAttributeError在此实例中被触发。 p>
否则,我想你可以创建自己的控制器,通过生成这样的东西:
code>#app / controllers / registrations_controller.rb
class RegistrationsController< Devise :: RegistrationsController
def new
super
end
def create
#添加自定义创建逻辑
end
def update
super
end
end
然后告诉设计使用该控制器而不是默认值:
#app / config / routes.rb
devise_for :users,:controllers => {:registrations => 注册}
I have added a field to the sign-up form that is based on a different model, see How do I use nested attributes with the devise model for the gory details. This part is working fine.
The problem now is when I save, it is failing in the create action of the registrations controller that is supplied by devise with an Activerecord::UnknownAttributeError
on this field (company).
I am assuming I need to override the registrations controller, or is there a better/easier way I should be approaching this?
In your form are you passing in any other attributes, via mass assignment that don't belong to your user model, or any of the nested models?
If so, I believe the ActiveRecord::UnknownAttributeError is triggered in this instance.
Otherwise, I think you can just create your own controller, by generating something like this:
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def new
super
end
def create
# add custom create logic here
end
def update
super
end
end
And then tell devise to use that controller instead of the default with:
# app/config/routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}
这篇关于覆盖设计注册控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!