简体   繁体   中英

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. But how do I add a field to the sign_up form that is not a part of the User model? 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:

<%= 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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