submit_tag,check_box_tag返回错误

时间:2017-09-26 17:47:31

标签: ruby-on-rails ruby


我在使用check_box_tag时遇到了一些问题,我的方法启用(可以禁用某个产品)在Ruby on Rails中。 我创建了一些复选框,以便用户选择他想要设置的产品enable = true。 继续返回错误"无法找到带有' id' =启用"

的产品

我的方法enable

def enable
   Product.update_all(["enable=?", true], :id => params[:selected_products])
   redirect_to root_url
end

我的routes.rb

Rails.application.routes.draw do
  get 'product_export/new'

  get 'product_imports/new'

  resources :users
  resources :product_imports
  resources :products do
    collection do
      post :import
      post :export
      post :enable
    end
  end

  root to: 'products#index'
end

最后我的view使用了表格check_box_tag:

<%= form_tag enable_products_path, :method => :put do %>
  <%= submit_tag "Enable products" %>
  <table class="table table-striped table-responsive" id="productsTable">
    <thead class="thead-inverse">
      <tr>
        <th/>
        <th>Code</th>
        <th>Enable</th>
        <th>Bar code</th>
        <th>Unit cost</th>
        <th>Description</th>
        <th>Family</th>
        <th>Final</th>
        <th>Measure</th>
        <th colspan="3"></th>
      </tr>
    </thead>
    <tbody>
      <% @products.each do |product| %>
        <tr>
          <td><%= check_box_tag 'selected_products[]', product.id %></td>
          <td><%= link_to (product.code), edit_product_path(product) %></td>
          <td><%= product.enable %></td>
          <td><%= product.bar_code %></td>
          <td><%= product.unit_cost %></td>
          <td><%= product.description %></td>
          <td><%= product.family %></td>
          <td><%= product.final %></td>
          <td><%= product.measure %></td>
          <td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
        </tr>
      <% end %>
    </tbody>
  </table>
<% end %>

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

使用where查找所选产品以及何时更新产品:

Product.where(id: params[:selected_products]).update_all(enable: true)