Victor Policastro Victor Policastro - 21 days ago 10
Ruby Question

submit_tag with check_box_tag returning error



I'm having some trouble with

check_box_tag
and my method enable (a product can be disabled) in Ruby on Rails.
I created some checkbox so the user select the products that he wants to set enable = true.
Keeps returning the error "Couldn't find Product with 'id'=enable"

My method
enable
:

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


My
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


and finally my
view
with the table e the 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 %>


Thanks for the help!

Answer Source

Use where to find selected products and when to update them:

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