有许多通过协会

时间:2017-10-25 06:33:05

标签: ruby-on-rails

我有以下协会,效果很好。

class Indicator < ApplicationRecord
  belongs_to :statement, required: false
  belongs_to :identity, required: false
end

class Statement < ApplicationRecord
  has_many :indicators   
  has_many :identities, :through => :indicators 
  accepts_nested_attributes_for :identities
end

class Identity < ApplicationRecord
  has_many :indicators   
  has_many :statements, :through => :indicators 
  accepts_nested_attributes_for :statements
end

我的表格如下。我希望能够在名为Phases的模型中添加模型。因此,对于每个statement,可以有许多identities,并且对于每个身份我想使用select标记关联phase,然后能够在show视图中调用。我不确定如何最好地实现这一点。

<%= form_with(model: statement, local: true) do |form| %>
 <table class="table table-bordered">
  <% hidden_field_tag "statement[identity_ids][]", nil %>
   <% Identity.all.each do |b| %>
    <tr>
     <td><%= check_box_tag "statement[identity_ids][]", b.id, @statement.identity_ids.include?(b.id) %></td>
     <td><%= b.description %></td>
     <td>CODE FOR PHASES</td>
  </tr>
  <% end %>
 </table>

 <div class="actions">
  <%= form.submit %>
 </div>
<% end %>

显示视图

<table>
 <th>Description</th>
 <th>Phase</th>
 <% @statement.identities.each do |l| %>
 <tr>
  <td><%= l.description %></td>
  <td><% l.phases.each do |n| %><%= n.name %><% end %></td>
  Even tried this and same result.
  <td><%= l.phases.pluck(:name) %></td>
 </tr>
 <% end %>
</table>

我在表格结构下面添加了一张图片,我想到(不确定是否可能)和最终产品。

enter image description here

1 个答案:

答案 0 :(得分:2)

应用/控制器/ statements_controller.rb

compile 'com.google.android:flexbox:0.2.7'

应用/模型/ statement.rb

class StatementsController < ApplicationController
  # ...
  private

  def statement_params
    params.fetch(:statement, {})
      .permit(indicators_attributes: [:id, :_destroy, :identity_id, :phase_id])
  end
end

应用/视图/语句/ _form.html.erb

class Statement < ApplicationRecord
  has_many :indicators
  has_many :identities, through: :indicators
  accepts_nested_attributes_for :indicators, allow_destroy: true, reject_if: :all_blank
end

enter image description here

<%= form_with(model: @statement, local: true) do |f| %>
  <table class="table table-bordered">
    <tr>
      <th>Indicator Description</th>
      <th>Phase</th>
      <th>Remove?</th>
    </tr>
    <% Identity.all.each do |identity| %>
      <%= f.fields_for :indicators, f.object.indicators.where(identity: identity).first_or_initialize do |ff| %>
        <%= ff.hidden_field :id %>
        <%= ff.hidden_field :identity_id %>
        <tr>
          <td><%= ff.object.identity.description %></td>
          <td><%= ff.select :phase_id, options_from_collection_for_select(Phase.all, :id, :name, ff.object.phase_id), include_blank: true %></td>
          <td><%= ff.check_box :_destroy %></td>
        </tr>
      <% end %>
    <% end %>
  </table>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

应用/视图/语句/ show.html.erb

<form action="/statements" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="authenticity_token" value="quj6ylAE1l+730pQ/YvOxkv1pfJlBb7yj0TPmb8GCkj7A90PRufHFzyIvw53zcFE1ZeyJB8TCZDqho9vJPpyUQ==">
  <table class="table table-bordered">
    <tbody><tr>
      <th>Indicator Description</th>
      <th>Phase</th>
      <th>Remove?</th>
    </tr>

        <input type="hidden" name="statement[indicators_attributes][0][id]" id="statement_indicators_attributes_0_id">
        <input type="hidden" value="1" name="statement[indicators_attributes][0][identity_id]" id="statement_indicators_attributes_0_identity_id">
        <tr>
          <td>ABC</td>
          <td><select name="statement[indicators_attributes][0][phase_id]" id="statement_indicators_attributes_0_phase_id"><option value=""></option>
<option value="1">Phase 1</option>
<option value="2">Phase 2</option></select></td>
          <td><input name="statement[indicators_attributes][0][_destroy]" type="hidden" value="0"><input type="checkbox" value="1" name="statement[indicators_attributes][0][_destroy]" id="statement_indicators_attributes_0__destroy"></td>
        </tr>

        <input type="hidden" name="statement[indicators_attributes][1][id]" id="statement_indicators_attributes_1_id">
        <input type="hidden" value="2" name="statement[indicators_attributes][1][identity_id]" id="statement_indicators_attributes_1_identity_id">
        <tr>
          <td>DEF</td>
          <td><select name="statement[indicators_attributes][1][phase_id]" id="statement_indicators_attributes_1_phase_id"><option value=""></option>
<option value="1">Phase 1</option>
<option value="2">Phase 2</option></select></td>
          <td><input name="statement[indicators_attributes][1][_destroy]" type="hidden" value="0"><input type="checkbox" value="1" name="statement[indicators_attributes][1][_destroy]" id="statement_indicators_attributes_1__destroy"></td>
        </tr>
  </tbody></table>

  <div class="actions">
    <input type="submit" name="commit" value="Create Statement" data-disable-with="Create Statement">
  </div>
</form>

enter image description here

经过测试的工作