无法在连接表中创建条目

时间:2015-12-03 09:27:27

标签: ruby-on-rails ruby-on-rails-4 activerecord jointable cocoon-gem

我试图将条目保存到连接表,但一直没有成功。正如我配置的那样,Stockholder has_many :entity_joins(我试图允许Stockholder由多个people组成,并允许Person参与多个stockholders。单个股东的每个加入条目都添加了cocoon,如下所示:

<h1>Stockholders#edit</h1>
<p>Find me in app/views/stockholders/edit.html.erb</p>
<%=  simple_form_for [@stock, @stockholder], html: {id:"stockholderform"}, update: { success: "response", failure: "error"} do |f| %>
    <div class="container">
        <div class="symegrid">
            <div class="form-inline">
                <%= simple_fields_for :entity_joins do |ejoin| %>
                    <%= render 'entity_join_fields', f:ejoin %>
                <% end %>
                <div>
                    <%= link_to_add_association 'Add Owner', f, :entity_joins, class: "btn btn-default add-button" %>
                </div>

entity_join_fields只包含一个字段,用于指示应通过选择菜单将哪个人添加到Stockholder

<%= f.grouped_collection_select :entity_id, [Org, Person], :all, :model_name, :to_global_id, lambda {|org_or_person_object| org_or_person_object.instance_of? Org? rescue org_or_person_object.fname + " " + org_or_person_object.lname rescue org_or_person_object.name}, label:"Stockholder", class: "names"%>

但是当我去保存Stockholder表单时,development.log中的参数如下:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"JF6a/hVU49y1AAX4iAyi6t/Y/ti1S6CAotw28Qj8PGbESpl/puMUliqOz9iwUQ9vHwIeF5hFjykwSUTcKpzGrQ==", "entity_joins"=>{"entity_id"=>"gid://legal/Person/2"}, "stockholder"=>{"cert_number"=>"", "issue_date(2i)"=>"12", "issue_date(3i)"=>"2", "issue_date(1i)"=>"2015", "shares_issued"=>"", "shares_repurchased"=>"", "shares_canceled"=>"", "shares_outstanding"=>""}, "commit"=>"Update Stockholder", "stock_id"=>"1", "id"=>"2"}

最引人注目的是entity_joins超出stockholder参数并且没有嵌套在其中。显然,它不会保存到数据库中,也不会抛出任何错误。 entity_joins的数据是正确的(它是Person的全局ID,因此我可以将连接扩展为多态,但这是另一个故事)。有什么明显可能导致这个问题吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

您需要在表单构建器实例上调用simple_fields_for

<%= f.simple_fields_for :entity_joins do |ejoin| %>
  <%= render 'entity_join_fields', f:ejoin %>
<% end %>

否则它没有上下文,只会使用name="entity_joins[something]"而不是stockholder[entity_joins][something]创建输入。