gpt4 book ai didi

ruby-on-rails - 当文本字段填充多对多表单时,Rails 3.2 Ajax 更新 Div

转载 作者:行者123 更新时间:2023-12-01 06:01:26 26 4
gpt4 key购买 nike

最后我想要一个将 client_id 传递给部分的文本字段。我想异步执行此操作,以便在更新文本字段值时,shipment_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

这和我最终想要的差不多。 enter image description here

最佳答案

由于我不知道你的 Controller 中有什么以及你如何制定路线,所以我提出了一些建议。根据实际情况更改一些内容。我假设您不需要更改 index 操作(如果只是添加 respond_to js)

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" %>");

关于ruby-on-rails - 当文本字段填充多对多表单时,Rails 3.2 Ajax 更新 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11159033/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com