简体   繁体   English

如何在Rails Devise中将字段添加到sign_up表单中,而不在User模型中添加字段?

[英]How do I add a field to the sign_up form in Rails Devise, but not the User model?

There are several answers out there on how to add a field to the sign_up form in Rails 4 if the field is also a new property of the User model. 如果该字段还是User模型的新属性,那么有几种答案可以解决如何在Rails 4中将字段添加到sign_up表单中。 But how do I add a field to the sign_up form that is not a part of the User model? 但是,如何将不属于用户模型的字段添加到sign_up表单中? In my specific example, I would like to add a Company Name field in the sign up form, which would then be used to create a Company model before the User model is created. 在我的特定示例中,我想在注册表单中添加“公司名称”字段,然后将其用于在创建用户模型之前创建公司模型。 How would I go about doing that? 我将如何去做?

Thank you both for the response. 谢谢你们的答复。

The answer was as simple as running the following to generate the devise controllers: 答案很简单,只需运行以下命令即可生成设计控制器:

rails generate devise:controllers users

Then updating the route: 然后更新路线:

devise_for :users, controllers: { registrations: 'users/registrations' }

Adding a form_tag to the form: 将form_tag添加到表单:

<%= text_field_tag("company_name", nil, {class: 'form-control'}) %>

Then updating the method in the registrations controller to create the company: 然后在注册控制器中更新方法以创建公司:

# POST /resource
def create
super do |resource|
  company_name = params[:company_name]
  company = Company.new({name: company_name})
  company.save!
end

end 结束

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM