【问题标题】:strong parameters has_many through强参数 has_man 通过
【发布时间】:2014-12-30 13:56:57
【问题描述】:

试图找出 setter 不起作用的原因:

class Company < ActiveRecord::Base
  has_many :memberships 
  has_many :users, :through => :memberships, dependent: :destroy

end

class Membership < ActiveRecord::Base
  belongs_to :user
  belongs_to :company

  validates_presence_of :user
  validates_presence_of :company

end

class User < ActiveRecord::Base
  has_many :memberships
  has_many :companies, :through => :memberships, dependent: :destroy
  accepts_nested_attributes_for :companies
end

<%= form_for(@user) do |f| %>
  <% if @user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>

      <ul>
      <% @user.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.fields_for :company do |ff|%>
      <%= ff.label :name %><br>
      <%= ff.text_field :name %>
    <% end %>
  </div>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

# Never trust parameters from the scary internet, only allow the white list through.
def user_params
  params.require(:user).permit(:name, companies_attributes:[:name])
end

当我这样做时它会起作用:

# Never trust parameters from the scary internet, only allow the white list through.
def user_params
  params.require(:user).permit(:name, company:[:name])
end

所以对我来说不是很清楚,因为文档说当接受_嵌套_attributes_for :companies 时 setter company_attributes 被 ceated

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    当使用fields_foraccepts_nested_attributes_for 时,它们需要匹配。见http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for

    您有accepts_nested_attributes_for :companiesf.fields_for :company 因此断开连接。

    所以尝试更改您的表单以包含

    <div class="field">
        <%= f.fields_for :companies do |ff|%>
          <%= ff.label :name %><br>
          <%= ff.text_field :name %>
        <% end %>
      </div>
    

    你的 user_params 方法是

    def user_params
      params.require(:user).permit(:name, companies_attributes:[:name])
    end
    

    这应该会导致它们在companies_attributes 下填充,并且您原来的user_params 方法可以工作。因为fields_for 应该生成user[companies_attributes[id][name]],因为user has_many companies(你想在要创建的列表中包括多个公司吗?)

    【讨论】:

    • 好吧,我使用了日志:) 但仍然没有得到它... 参数:{"utf8"=>"✓", "authenticity_token"=>"p08a+oGeMwhECryeQfDyO3th1lLarapkXKQtKb15nWZnD5kGU6o5zJLdTVlBmKK99dpmI4Pczap188P+Uuvqwk=== ", "user"=>{"company"=>{"name"=>"fff"}, "name"=>"ffff"}, "commit"=>"Create User"} 不允许的参数:公司 "user_params {\"name\"=>\"ffff\"}" 不允许的参数:公司
    • :companies 没有为 company.name 生成表单域
    • 请参阅我关于更改字段以匹配关联/accepts_nested_attributes_for 的答案的另一部分。
    • 它是一个 has_many ,所以它应该为 user[companies_attributes[id][name]] 生成它
    • 我的回答也只适用于has_many,因为它是针对has_many :through的,请查看这个答案,因为它可能对stackoverflow.com/questions/21983131/…有更多帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多