Search code examples
ruby-on-railsdeviseuser-management

Devise: Associated Model on Sign Up: Can't mass-assign


I setup Devise on User model with its default options.

I also have a Company model that needs to add data added whenever a user registers. A company owner setups a User to login with and a Company profile, both in the same form.

I setup Company model with has_many :users and User with has_one :company, but I keep getting Can't mass-assign protected when submitting the form. I followed Profile model for Devise users? and others from Stackoverflow, but no luck.

How can I setup the User and Company model to add the necessary data whenever a user registers? User data to User and company data to Company.


Solution

  • How I got it working:

    My User model has:

    attr_accessible :company_attributes
    
    belongs_to :company
    accepts_nested_attributes_for :company
    

    My Company model has:

    has_many :users
    

    And the new.html.erb from devise/registrations is with the following added:

    <% resource.build_company %>
    <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
    [ fields for User ]
    <%= f.fields_for :company_attributes, resource.company do |company| %>
    [ fields for Company ]
    <% end %>
    <% end %>