简体   繁体   English

Rails 4如何将动态参数传递给控制器​​?

[英]Rails 4 how to pass dynamic params to controller?

I have this form in my view: 我认为这种形式:

<%= form_tag({:action => :update_quantity}, :params => [:ids_to_update]) do %>
<% @id_array = [] %>
<% @db_lines.each do |line| %>
<% @id_array << line['id'] %>
    <tr>
      <td><%= image_tag "#{line['preview_url']}&maxSize=135" %></td>
      <td><%= line['ProductCode'] %></td>
      <td><%= text_field_tag("updated_quantity#{line['id']}", "#{line['Quantity']}") %></td>
      <td><%= number_to_currency(line['UnitPrice']) %></td>
      <td><%= number_to_currency(line['Quantity'] * line['UnitPrice']) %>
    </tr>
<% end %>
    <tr>
    <td colspan="5" class="text-right">
    <%= hidden_field_tag :ids_to_update, @id_array.join(',') %>
    <%= submit_tag("Update Quantity", :class => 'tiny button raidus') %>
    </td>
    </tr>
<% end %>

In the text_field_tag i'm setting each name to be updated_quantityx where x is the ID of the record. 在text_field_tag中,我将每个名称设置为update_quantityx,其中x是记录的ID。 How do I pass each one of those into the params section of the form_tag when I don't know what the ID will be and how many will be there? 当我不知道ID是多少以及有多少ID时,如何将其中的每一个传递到form_tag的params部分中? The records are coming from my order_line model and this page is a separate controller for the shopping cart.. 记录来自我的order_line模型,此页面是购物车的单独控制器。

You don't need the params section in the form_tag . 您不需要form_tagparams部分。 Params will be automatically generated by _tags inside the form. 参数将由_tags内的_tags自动生成。

The text_field_tag values will be passed to the controller as params[:updated_quantity#1] , params[:updated_quantity#2] and so on. text_field_tag值将作为params[:updated_quantity#1]params[:updated_quantity#2]等传递给控制器​​。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM