Hello group,
I'm new to Rails and i'm trying to create a 'nested' form for user to a company. I would like the client to be able to create a company in the users sign up form. I get the part were you can choose a company from a collection, but i want to allow the user to create a new company through a text field in the sign up user form.
This is what i have:
class User < ActiveRecord::Base belongs_to :company has_one :company
attr_accessible :email, :password, :password_confirmation, :remember_me, :admin, :company_id, :firstname, :lastname
accepts_nested_attributes_for :company end
class Company < ActiveRecord::Base has_many :users accepts_nested_attributes_for :users
attr_accessible :name
#validates_presence_of :name #validates_uniqueness_of :name end
and in the form:
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %> <%= f.error_notification %>
<div class="inputs"> <%= f.fields_for :company do |cf| %> <%= cf.input :name, :required => true, :autofocus => true %> <% end %> <%= f.input :email, :required => true, :autofocus => true %> <%= f.input :password, :required => true %> <%= f.input :password_confirmation, :required => true %> </div>
<div class="form-actions"> <%= f.button :submit, :class => 'btn-primary', :value => 'Sign up' %> </div> <% end %>
The above code gives a company name text field in the form but on submit rails trows an error: Can't mass-assign protected attributes: company. I probably have to add a route to? until now i only have resources :companies and resources :users.
I did my best to google for a solution/ example.. found some nested form examples but its not clear to me.
Any help on this would be appreciated.
Thanks in advance!