如何使用带有rails 4的设计将数据存储在第二个模型上?

时间:2013-09-18 21:48:31

标签: ruby-on-rails devise

有一些类似的问题,但答案不明确。 我想使用设计我能够添加更多字段并将它们存储在用户表中。 如果我想在注册过程中收集属于另一个表的数据 是可以通过设计控制器来完成它还是我必须自己写一个?

1 个答案:

答案 0 :(得分:0)

你必须使用自己的。覆盖注册控制器并将其添加到您的路由中,以便将您的设备注册指向新的注册控制器。

# 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 

然后将其添加到您的路线

# app/config/routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}

根据您构建表单的方式,您可能需要将额外字段添加到设计模型(通常为User) - 如果必须attr_accessor :field_name执行此操作>