【问题标题】:Conditional hidden tag based on checkbox condition in Rails基于Rails中复选框条件的条件隐藏标签
【发布时间】:2017-06-30 21:56:41
【问题描述】:

如何根据复选框是否被选中发送隐藏标签?

我有一个表格,其中包含产品标题和产品价格以及每行和提交表单上的复选框选择,我想将这两个值都发送到控制器。我只能通过一个复选框获得一个值来提交,所以我添加了一个隐藏标签字段,但是,这个隐藏标签将提交每一行,这不是我想要的。在下面的 params sent 示例中,它应该发送两个项目和两个价格,但它发送每一行的所有价格。 (顺便说一句,如果有更好的方法在不使用隐藏标签的情况下发送两个参数,请告诉我!)

这是来自谷歌分析 API 报告请求的数据,仅供参考 =>

@product_revenue.reports[0].data.rows
p.dimensions[0] = "Product Title"
p.metrics[0].values[0] = "Product Price"

这个结构来自here

查看代码:

<div class="col-md-6">
  <%= form_tag add_multiple_path, method: :post do %>
  <table>
    <thead>
      <th><strong>Product Title</strong></th>
      <th><strong>Product Price</strong></th>
      <th><strong>Add?</strong></th>
    </thead>
    <% @product_revenue.reports[0].data.rows.each do |p| %>
      <tr>
        <td><%= p.dimensions[0] %></td>
        <td><%= p.metrics[0].values[0] %></td>
        <td>
          <%= check_box_tag 'price_test_datum[product_title][]', p.dimensions[0] %>
          <%= hidden_field_tag('price_test_datum[product_price][]', p.metrics[0].values[0]) %>
        </td> 
      </tr>
    <% end %>
  </table>
  <%= submit_tag "Add selected" %>
  <% end %>
</div>

隐藏字段正在转储所有列值,而不是与该行关联的值?

发送的参数:

{
  "utf8"=>"✓", 
  "authenticity_token"=>"token here", 
  "price_test_datum"=>{
    "product_price"=>["29.98", "14.99", "14.99", "14.99", "14.99", "14.99", "299.95", "35.97", "21.98", "10.99", "33.98", "27.98", "13.99", "59.99", "29.98", "59.98", "29.99", "110.93", "4088.79"], 
    "product_title"=>["Turquoise Bracelets", "Red Bracelets"]
  }, 
  "commit"=>"Add selected"
}

【问题讨论】:

  • 请显示@product_revenue数据的一部分...
  • 这个'price_test_datum[product_price][]' 看起来很时髦——尤其是结尾的[]'price_test_datum[product_price] 似乎应该可以解决问题。我不认为这是你的问题,但它看起来很奇怪。
  • 添加了该数据并包含了表格的图像
  • 另外,奇怪的是您的视图代码有 &lt;th&gt;&lt;strong&gt;Product Price&lt;/strong&gt;&lt;/th&gt;,但您粘贴的图像显示“产品收入”。您确定您正在编辑正确的文件吗?
  • 我正在尝试构建参数,以便 params[:price_test_datum][:product_price] = 产品价格和 params[:price_test_datum][:product_title] = 产品标题。这部分还没搞清楚

标签: ruby-on-rails


【解决方案1】:

所以我在表格循环中添加了一个索引,并使用复选框值将相关的行值(索引)提交给控制器。然后,我使用隐藏标签字段将所有产品标题和价格值作为数组发送到控制器,并使用行键查找相关值。这似乎是一个不优雅的解决方案,但它确实有效。

<%= form_tag add_multiple_path, method: :post do %>
<table>
  <thead>
    <th><strong>Product Title</strong></th>
    <th><strong>Product Price</strong></th>
    <th><strong>Add?</strong></th>
  </thead>
  <% @product_revenue.reports[0].data.rows.each_with_index do |p, index| %>
    <tr>
      <td><%= p.dimensions[0] %></td>
      <td><%= p.metrics[0].values[0] %></td>
      <td>
        <%= check_box_tag 'row[]', index %>
        <%= hidden_field_tag 'price_test_datum[product_title][]', p.dimensions[0] %>
        <%= hidden_field_tag 'price_test_datum[product_price][]', p.metrics[0].values[0] %>
      </td> 
    </tr>
  <% end %>
</table>

控制器代码

def add_multiple
params[:row].each do |r|
  PriceTestDatum.create(product_title: params[:price_test_datum][:product_title][r.to_i],
                        product_price: params[:price_test_datum][:product_price][r.to_i])
end
respond_to do |format|
  format.html { redirect_to price_test_data_path }
  format.json { head :no_content }
end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 2013-02-04
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多