2

最後に、client_id をパーシャルに渡すテキスト フィールドが必要です。textfield の値が更新されると、shipping_products パーシャルが動的に変更されるように、これを非同期で行いたいと思います。これを行う最善の方法は何ですか?

index.html.erb で

<!-- Text Field Here-->
<div id="available_products">
  <%= render "shipment_products" %>
</div>

_shipment_products.html.erb 内

<div id="shipment_products_container">
  <h3>Assign Products to Ship<\h3>
  <ul class="shipment_products" id="shipment_products">
    <% Product.by_client(client_id).each do |product|%>  <!-- TextField value passed here -->
      <%= content_tag_for :li, product, :value => product.id do %>
        <%= hidden_field_tag("shipment[product_ids][]", product.id) %>
        <%= product.product_name %>
      <% end %>
    <% end %>
  <\ul>
</div>

モデル関係:

Models and Relationships
  Shipment  has_many :products :through => :shipment_products
  Product   has_many :shipments :through => :shipment_products
  ShipmentProducts belongs_to :shipment,  belongs_to :product
  Product belongs_to :client
  Client has_many :products

これは、私が最終的に望むものと似ています。ここに画像の説明を入力

4

2 に答える 2

0

これにはネストされたモデルの概念を使用する必要があると思います-参照してください-

http://railscasts.com/episodes/197-nested-model-form-part-2?view=asciicast

于 2012-06-30T08:47:49.987 に答える
0

コントローラーの内容とルートの作成方法がわからないので、いくつかの提案をしました。実際のものにいくつか変更します。アクションを変更する必要はないと思います( jsindexを追加するだけの場合)respond_to

index.html.erb

<%= form_tag shipment_path, remote: true do %> - points to index action
  <%= text_field_tag :client_id %>
  <%= submit_tag :load %>
<% end %>
<div id="available_products">
  <%= render "shipment_products" %>
</div>

index.js.erb

$('#available_products').html("<%= j render "shipment_products" %>");
于 2012-06-25T06:04:18.177 に答える