I have challenge to find way how to submit value in 'company_profiles' table for 'Company' and 'company' is polymorphic to User. Already tried so many ways and no luck. Always getting - 'Unpermitted parameter: company_profiles'. Thanks in advance for any help.
..app/controllers/application_controller
protect_from_forgery with: :exception <br>
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:company_id, :company_profiles_attributes => [ :first_name_legal]) }
.app/controllers/companies/registrations_controller.rb
# GET /resource/sign_up
def new
build_resource({})
resource.company_profile
set_minimum_password_length
yield resource if block_given?
respond_with self.resource
end
# POST /resource
def create
build_resource(sign_up_params)
resource.save
yield resource if block_given?
if resource.persisted?
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_flashing_format?
sign_up(resource_name, resource)
respond_with resource, location: after_sign_up_path_for(resource)
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
expire_data_after_sign_in!
respond_with resource, location: after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
set_minimum_password_length
respond_with resource
end
end
def sign_up_params
allow = [:company_id, :email, :password, :password_confirmation,
:company_profiles_attributes => [:first_name_legal]]
params.require(resource_name).permit(allow)
end
Models:
..app/models/user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :role, polymorphic: true
belongs_to :company_profile
accepts_nested_attributes_for :company_profile
validates_uniqueness_of :email
end
..app/models/company.rb
class Company < User
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :user, :as => :role
belongs_to :company_profile
accepts_nested_attributes_for :company_profile
accepts_nested_attributes_for :user
end
..app/models/company_profile.rb
class CompanyProfile < ActiveRecord::Base
has_many :users
has_many :companies
accepts_nested_attributes_for :users
accepts_nested_attributes_for :companies
end
Registration form:
..app/views/companies/new.html.erb
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |fc| %>
<%= devise_error_messages! %>
<div class="field">
<%= fc.label :email %><br/>
<%= fc.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= fc.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= fc.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= fc.label :password_confirmation %><br />
<%= fc.password_field :password_confirmation, autocomplete: "off" %>
</div>
<%= fc.fields_for :company_profiles do |fa| %>
<%= fa.text_field :first_name_legal, :class => "form-control", :id => "exampleInputName1", :placeholder => "*First name", :required => true, :maxlength => 35 %>
<% end %>
<div class="actions">
<%= fc.submit "Sign up" %>
</div>
<% end %>
Server logs
Started POST "/companies" for 127.0.0.1 at 2015-07-30 12:17:19 -0700 Processing by Companies::RegistrationsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"nG3FmJOunzJfekYo0LE8OOK6rR9lp8kjW6pbXJl6N2QggiZsmrS/N+KU/r6IfwSVUep0Elmiir+d/w+vULTrQw==", "company"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "company_profiles"=>{"first_name_legal"=>"super man company"}}, "commit"=>"Sign up"}
Unpermitted parameter: company_profiles
I finally found an answer if anyone will have this issue try following:
1- Change everything to singular.
2- Change - GET /resource/sign_up
def new
build_resource({})
resource.company_profile = CompanyProfile.new
....3- Update - class CompanyProfile < ActiveRecord::Base
belongs_to :company
accepts_nested_attributes_for :company
end4- Update - class Company < User
has_many :company_profiles
accepts_nested_attributes_for :company_profiles
end5- And add - company_profile_id to user table
And all works