简体   繁体   English

如何将devise与rails 4一起用于在第二个模型上存储数据?

[英]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.

相关问题 如何为Ruby on Rails配置Devise以将电子邮件和密码存储在除用户模型之外的其他位置? - How can I configure Devise for Ruby on Rails to store the emails and passwords somewhere other than in the user model? 如何在Rails 4模型中存储符号? - How can I store a symbol in a Rails 4 model? 如何在没有数据库的情况下使用Rails / Devise? - How can I use Rails/Devise without a database? 我可以在不同于Rails 3中主要身份验证模型的模型上使用token_authenticable(来自Devise)吗? - Can I use token_authenticable (from Devise) on a different model than my main authentication model in Rails 3? 如何创建第二个Rails内存存储缓存? - How can I create a second Rails in-memory store cache? 如何使用devise的“ warden”对与使用devise的rails应用程序位于同一堆栈中的机架应用程序进行身份验证? - How can I use devise's “warden” to authenticate a rack app in the same stack as the rails app that uses devise? rails,devise gem-如何配置devise控制器? - rails, devise gem - How can I configure devise controller? 如何在Rails中存储模型数据的版本? - How do I store versions of model data in rails? 如何为rails_admin使用多设计模型 - How to use multi devise model for rails_admin Rails4:如何对检索到的Model.all数据使用截断方法? - Rails4: How can I use truncate method for the data retrieved Model.all?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM