rails 4 多用户模型 (STI) 多态 - 额外配置文件的嵌套属性 table

rails 4 Multiple user models (STI) polymorphic - nested attribute to extra profile table

我很难找到如何在 'company_profiles' table 中为 'Company' 提交值的方法,并且 'company' 对用户来说是多态的。已经尝试了很多方法,但没有运气。总是得到 - 'Unpermitted parameter: company_profiles'。在此先感谢您的帮助。

..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"=>"some@superman.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "company_profiles"=>{"first_name_legal"=>"super man company"}}, "commit"=>"Sign up"}

Unpermitted parameter: company_profiles

您在 company_profile 上与 company 建立了 belongs_to 关联。在整个代码中将 company_profiles 更改为 comapny_profile

我终于找到了答案,如果有人遇到这个问题,请尝试以下操作:

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
end

4- Update - class Company < User
has_many :company_profiles
accepts_nested_attributes_for :company_profiles
end

5- And add - company_profile_id to user table

所有作品