我的视图:
<%= form_tag(update_user_products_path, :id => "update_user_form") do %>
<%@products.each do |product| %>
<div class = "prod">
<a href='<%= recommendations_path(:product_id => product.id, :rating_set_id => params[:rating_set_id]) %>' target="_blank"><img src='<%= product.cover_img %>' class='product_image_prods'></img></a>
<div class= "title"><small><b><%= link_to truncate(product.title, :length =>30), recommendations_path(:product_id => product.id, :rating_set_id => params[:rating_set_id]), :target => '_blank' %></b></small></div>
<br/>
<div><em>Current Rating: <%= product.rating %> </em></div>
<%= select_tag "user_id:#{product.id}", options_for_select(User.all.collect {|u| [ u.name, u.id ] })%>
</div>
<% end %>
<%= submit_tag %>
<% end %>
我需要向我的控制器更新操作发送2个参数——每个记录的product.id和所选记录的user.id。调用params.keys返回:
utf8
authenticity_token
user_id:1
user_id:2
user_id:3
user_id:4
...
user_id:78
commit
action
controller
我可以通过(":")拆分params.keys。这样做会返回与params.keys相同的输出。是否有其他方法可以将product.id传递给我的update_user操作?
您可以将选择的名称更改为
<%= select_tag :user_id, options_for_select(User.all.collect {|u| [ u.name, u.id ] }), name: "users[#{product.id}]" %>
它将为params[:users]
提供一个散列,其中键是product_ids,值是user_ids