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

时间:2015-07-30 19:20:02

标签: ruby-on-rails-4 devise nested nested-attributes sti

我很难找到如何在'company_profiles'表中为'Company'提交值的方法,'company'是多态的User。已经尝试了很多方法而没有运气。总是得到 - '未经许可的参数: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 %>


  

服务器日志

     

在2015-07-30 12:17:19 -0700开始发布“/ companies”for 127.0.0.1   公司处理:: RegistrationsController #create as HTML     参数:{“utf8”=&gt;“✓”,“authenticity_token”=&gt;“nG3FmJOunzJfekYo0LE8OOK6rR9lp8kjW6pbXJl6N2QggiZsmrS / N + KU / r6IfwSVUep0Elmiir + d / w + vULTrQw ==”,“company”=&gt; {“email”=&gt; “some@superman.com”,“password”=&gt;“[FILTERED]”,“password_confirmation”=&gt;“[FILTERED]”,“company_profiles”=&gt; {“first_name_legal”=&gt;“超人公司” },“commit”=&gt;“注册”}

     

未经许可的参数:company_profiles

2 个答案:

答案 0 :(得分:0)

Workbook wkActive = Globals.ThisAddIn.Application.ActiveWorkbook; Microsoft.Office.Interop.Excel.Workbook workbook = Globals.ThisAddIn.Application.Workbooks.Open(IdsTemplatePath, 0, true, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true, false, false); Microsoft.Office.Interop.Excel.Worksheet worksheet = workbook.Sheets[1] as Microsoft.Office.Interop.Excel.Worksheet; worksheet.Copy(Type.Missing, wkActive); wkActive.Save(); Exception from HRESULT: 0x800A03EC belongs_to之间存在company_profile关联。在整个代码中将company更改为company_profiles

答案 1 :(得分:0)

如果有人遇到此问题,我终于找到了答案:

  

1-将所有内容改为单数。

     

2-变化 -       GET / resource / sign_up
      def new
      build_resource({})
      resource.company_profile = CompanyProfile.new
  ....

     

3-更新 - 类CompanyProfile&lt; ActiveRecord :: Base

  belongs_to:company
accepted_nested_attributes_for:company
  结束

     

4-更新 - 类公司&lt;用户
has_many:company_profiles   
accepts_nested_attributes_for:company_profiles
结束

     

5-并将 - company_profile_id添加到用户表

所有作品