【问题标题】:Unable to create entry in join table无法在连接表中创建条目
【发布时间】:2015-12-03 09:27:26
【问题描述】:

我正在尝试将条目保存到连接表,但没有成功。正如我配置的那样,Stockholderhas_many :entity_joins(我试图让Stockholder 包含多个people,并允许Person 参与多个stockholders。每个加入条目用于单个股东加茧如下:

<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,因此我可以将连接扩展为多态,但这是另一回事)。是否有任何明显的原因可能导致此问题?

提前致谢。

【问题讨论】:

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


    【解决方案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]创建输入。

    【讨论】:

    • 哈哈!哦,男孩,我认为这是我该睡觉的标志了。非常感谢。 :-P
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    相关资源
    最近更新 更多