[英]How can I use devise with rails 4 to store data on a second model?
There are some similar questions but not clear answer. 有一些类似的问题,但不清楚的答案。 I want to use devise I was able to add more fields and store them on the user table. 我想使用devise,我能够添加更多字段并将其存储在用户表中。 If I want to collect data during the signup process that belong to another table is it possible to do it through devise controller or do I have to write my own one ? 如果我想在注册过程中收集属于另一个表的数据,是否可以通过devise控制器来完成,还是必须编写自己的数据?
You have to use your own. 您必须使用自己的。 Override the registrations controller and add it to your routes to point your devise registration to your new registrations controller. 覆盖注册控制器并将其添加到您的路线中,以将您的设备注册指向新的注册控制器。
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def new
super
end
def create
# add custom create logic here
# Strip the fields out of the resource params and save them to
# another model then you can call super to have the method behave as it normal:
super
end
def update
super
end
end
Then add it to your routes 然后将其添加到您的路线
# app/config/routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}
Depending on how you're structuring your form you might need to add the extra fields to the devise model (typically User
) - you can use attr_accessor :field_name
to do this if you must 根据您构造表单的方式,可能需要向设计模型中添加额外的字段(通常为User
)-如果必须,可以使用attr_accessor :field_name
来执行此操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.