【问题标题】:Rails 4 how to pass dynamic params to controller?Rails 4 如何将动态参数传递给控制器​​?
【发布时间】:2014-03-15 19:32:01
【问题描述】:

我认为有这个表格:

<%= 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 %>

在 text_field_tag 中,我将每个名称设置为 updated_quantityx,其中 x 是记录的 ID。当我不知道 ID 是什么以及会有多少时,如何将其中的每一个传递到 form_tag 的 params 部分?记录来自我的 order_line 模型,此页面是购物车的单独控制器..

【问题讨论】:

    标签: ruby-on-rails parameters params form-for


    【解决方案1】:

    您不需要form_tag 中的params 部分。 参数将由表单内的_tags 自动生成。

    text_field_tag 值将作为params[:updated_quantity#1]params[:updated_quantity#2] 等传递给控制器​​。

    【讨论】:

      猜你喜欢
      • 2013-03-24
      • 1970-01-01
      • 2016-05-11
      • 2020-05-14
      • 1970-01-01
      • 2012-12-18
      • 1970-01-01
      • 2020-05-09
      • 2018-07-14
      相关资源
      最近更新 更多