【问题标题】:rails 4 Multiple user models (STI) polymorphic - nested attribute to extra profile tablerails 4多用户模型(STI)多态-嵌套属性到额外的配置文件表
【发布时间】:2015-07-30 19:20:01
【问题描述】:

我很难找到如何在“company_profiles”表中为“公司”提交值的方法,并且“公司”对用户来说是多态的。已经尝试了很多方法,但没有运气。总是得到-'未经允许的参数: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 年 7 月 30 日 12:17:19 -0700 开始为 127.0.0.1 发布“/companies” 公司处理::RegistrationsController#create as HTML 参数:{"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"=> “注册”}

不允许的参数:company_profiles

【问题讨论】:

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


    【解决方案1】:

    您在company_profile 上有一个belongs_to 关联,用于company。将整个代码中的company_profiles 更改为comapny_profile

    【讨论】:

    • 到处都更改为 company_profile,现在注册屏幕上根本没有显示该字段,知道吗?
    【解决方案2】:

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

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

    2- 改变- 获取 /resource/sign_up
    定义新
    build_resource({})
    resource.company_profile = CompanyProfile.new
    ....

    3- 更新 - 类 CompanyProfile <:base>
    属于_to :company
    接受_nested_attributes_for :company
    结束

    4- 更新 - 类公司 has_many :company_profiles
    accept_nested_attributes_for :company_profiles
    end

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

    一切顺利

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多