【问题标题】:How can save the information selected using ajax?如何保存使用ajax选择的信息?
【发布时间】:2014-06-03 23:09:40
【问题描述】:

我使用 rails 做了一个应用程序,当我点击产品图标时会显示选择的产品,但总是替换产品,我正在尝试保持产品选择。

这里是控制器:

def index
   @products= CustomerProduct.all
end 

def selected_product
   @selected_product = CustomerProduct.find(params[:id])
end

这是索引视图:(显示所有产品和将更新的 div)

<% @products.each do |product| %>
 <p><%= product.product_code %>
    <%= product.product_name %>
    <%= product.product_price %>
    <%= link_to_remote image_tag("image.png"),:url=>{:controller=>"customer_product",:action=>"selected_product",:id=>product.id } %>
 </p>
<% end %>

<div id="list_products">
   ## Here is the div that will update after select a product.
</div> 

这里是替换 div 的 ajax 更新:“selected_product.rjs”

 page.replace_html("list_products", :partial=>"customer_product/partials/add_product")

这是显示所选信息的部分视图 add_product

<% @products.each do |product| %>
 <p><%= product.product_code %>
    <%= product.product_name %>
    <%= product.product_price %>
  </p>
 <% end %>

工作正常,但总是替换 div list_products

有什么方法可以在我每次选择产品时添加选择的产品?

请有人可以帮助我吗?

非常感谢您的帮助。

【问题讨论】:

  • 你尝试在这里通过 AJAX 发送到哪里..?
  • page.replace_html 似乎按预期工作
  • Yes 总是在替换并且正在工作....但我想保留我选择的产品,就像图像示例一样

标签: javascript jquery ruby-on-rails ruby ruby-on-rails-2


【解决方案1】:

你可以做这样的事情,而不是使用page.replace_html

page.insert_html(:bottom, "list_products", :partial=>"customer_product/partials/add_product")

【讨论】:

  • 该死...你真聪明... =)谢谢你的回答大师。
  • 我有一个最后一个问题,如何删除选中的内容?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-09
  • 1970-01-01
  • 2018-01-21
  • 2021-02-23
相关资源
最近更新 更多