【问题标题】:Nested form in Rails not savingRails中的嵌套表单不保存
【发布时间】:2015-05-24 22:03:42
【问题描述】:

我正在使用带有 cocoon gem 的 Rails 4.2 来构建嵌套表单。一切都保存和显示都很好,除非我想通过entities 制作新的person 表单。目标是以我将创建新实体的相同形式即时创建新人。

代码如下:

型号:

class Entity < ActiveRecord::Base  
  has_many :bonds
  has_many :personas, :through => :bonds

  accepts_nested_attributes_for :bonds, allow_destroy: true, reject_if: :all_blank
  accepts_nested_attributes_for :personas, allow_destroy: false, reject_if: :all_blank
end

class Bond < ActiveRecord::Base
  belongs_to :entity
  belongs_to :persona
end

class Persona < ActiveRecord::Base
  has_many :bonds
  has_many :entities, :through => :bonds

  accepts_nested_attributes_for :entities
end

这是我的表单,除了我尝试添加新的person 时。

/views/entities/_form.html.erb

<%= simple_form_for @entity do |f| %>

    <h3>Info:</h3>
        <%= f.label :name %>
        <%= f.text_field :name %>

        <%= f.label :number %>
        <%= f.number_field :number %>

    <h3>Bonds:</h3>
    <div id="bonds">
      <%= f.fields_for :bonds do |ff| %>
      <%= render 'bond_fields', :f => ff %>
      <% end %>
      <div class="links">
        <%= link_to_add_association 'add bond', f, :bonds %>
      </div>
    </div>

      <%= f.submit %>
  <% end %>

/entities/_bond_fields.html.erb

<div class='nested-fields'>
    <%= f.label "Persona: " %>
    <div class="persona">
      <div class="persona_from_list">
      <%= f.association :persona, :collection => Persona.order(:name), :prompt => 'Choose an existing owner' %>
      </div>
      <%= link_to_add_association "add a new person as owner", f, :persona %>
    </div>

    <%= f.label "how much: " %>
    <%= f.number_field :how_much %>

    <%= f.label "percent: " %>
    <%= f.number_field :percent %>

  <%= link_to_remove_association "remove bond", f %>
</div>

/entities/_persona_fields.html.erb

  <div class='nested-fields'>
      <%= f.label "Name: " %>
      <%= f.text_field :name %> 

      <%= f.label "Code: " %>
      <%= f.text_field :p_code %>

      <%= link_to_remove_association "chose from list", f %>
  </div>

我在所有地方都添加了强大的参数,所以这可能不是问题。也许你可以帮助我不知道出了什么问题!

这是我在 /entities_controller 中的参数

params.require(:entity).permit(:name, :number, :address, :registered_at, 
:bonds_attributes => [:id, :persona_id, :entity_id, :percent, :date, :_destroy, 
:personas_attributes => [:id, :name, :p_kods, :living ] ], 
:personas_attributes => [:id, :name, :p_kods, :living ] )

还是谢谢...

【问题讨论】:

  • 强参数仍然可能是问题所在,但除非您向我们展示您的参数方法,否则无法确定。
  • 您的entities_controller 强参数中有:personas_attributes =&gt; [ ... ] 吗?
  • 是的,我在entities_controller 中有:personas_attributes。我在我的问题中添加了整个强大的参数。你可以看看。

标签: ruby-on-rails ruby-on-rails-4 nested associations nested-forms


【解决方案1】:

仔细阅读服务器日志后,我看到有Unpermitted parameter: persona_attributes

所以解决方案只是将EntitiesController 中的personas_attributes 更改为persona_attributes。奇怪的是,当我写复数时,rails 正在传递 Persona 单数的参数。

希望它也能帮助其他人......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多