Rails controller issue when updating multiple records


Rails controller issue when updating multiple records



I am trying to update multiple records using checkboxes.



I manage to display the edit_multiple page properly, but when i validate it, I have the error. I suspect it has to do with the controller, looking at the error message.(The column Group_id should be updated for each of the selected row)


Group_id



Anybody knows what is wrong ?



Thank you



The error:


ActiveRecord::RecordNotFound in DistribsController#update_multiple
Couldn't find Distrib with 'id'=
Extracted source (around line #75):

def update_multiple
@distribs = Distrib.find(params[:distrib_ids])
@distribs.reject! do |distrib|
distrib.update_attributes(params[:distrib_ids].reject {|k, v| v.blank?})
end
Application Trace | Framework Trace | Full Trace
app/controllers/distribs_controller.rb:75:in `update_multiple'
Request
Parameters:

{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"NupBi81NJtyOnM4rE/p4K99Kp7f6ClW9i+dnmVN1alo0ItCgQvNW46vYvA8hHaTe8shL/Wc8L5CTpqlHtsJedg==",
"distrib"=>{"group_id"=>"1"},
"commit"=>"Update multiple",
"locale"=>"en"}


class DistribsController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]

respond_to :html, :json

def edit_multiple
@distribs = Distrib.find(params[:distrib_ids])
end

def update_multiple
@distribs = Distrib.find(params[:distrib_ids])
@distribs.reject! do |distrib|
distrib.update_attributes(params[:distrib_ids].reject {|k, v| v.blank?})
end
if @distribs.empty?
redirect_to distribs_url
else
@distrib = Distrib.new(params[:distrib_ids])
redirect_to distribs_path
end
end
end


<%= form_tag update_multiple_distribs_path, method: :put do %>
<%= fields_for :distrib do |f| %>
<ul>
<% for distrib in @distribs %>
<li>
<% hidden_field_tag "distrib_ids", distrib.id %>
<%= h distrib.first_name %>
</li>
<% end %>
</ul>
<p>
<%= f.label :group_id %> <br/>
<%= f.collection_select :group_id, Group.all, :id, :name %>
</p>
<div class="actions">
<%= submit_tag "Update multiple" %>
</div>
<% end %>
<% end %>


resources :distribs do
collection do
post :edit_multiple
put :update_multiple
end
end


<%= form_tag edit_multiple_distribs_path %>

<table class="table table-hover">
<thead>
<th scope="col"> Id</th>
<th scope="col"> First Name</th>
<th scope="col"> Last Name</th>
<th scope="col"> Email</th>
<th scope="col"> Company</th>
<th scope="col"> Select</th>
<th scope="col"> Ind. Delete</th>

</thead>
<% @distribs.each do |distrib| %>
<tbody>
<tr>
<th scope="row"> <%= distrib.id %> </th>
<td> <%= best_in_place distrib, :first_name %>
</td>
<td> <%= best_in_place distrib, :last_name %>
</td>
<td> <%= best_in_place distrib, :email %>
</td>
<td> <%= best_in_place distrib, :company %>
</td>
<td>


<%= check_box_tag "distrib_ids", distrib.id %>
</td>
<td>
<%= link_to (t :Delete), distrib_path(distrib), method: :delete, data: {confim: "You will be deleting this message, Are you sure ?"}, class: "btn btn-default" %>
</td>
</tr>
</tbody>
<% end %>
</table>
<%= submit_tag "Add to mailist" %>
</div>





Try replacing <%= form_tag edit_multiple_distribs_path, method: :put do %> with <%= form_tag update_multiple_distribs_path, method: :put do %>.
– Jagdeep Singh
Jun 30 at 9:27


<%= form_tag edit_multiple_distribs_path, method: :put do %>


<%= form_tag update_multiple_distribs_path, method: :put do %>





Hi Jagdeep, i tried, and have the following : ActiveRecord::RecordNotFound (Couldn't find Distrib with 'id'=): app/controllers/distribs_controller.rb:75:in update_multiple' row 75 of the controller is : @distribs = Distrib.find(params[:distrib_ids])`
– Etienne
Jun 30 at 9:40


ActiveRecord::RecordNotFound (Couldn't find Distrib with 'id'=):


update_multiple' row 75 of the controller is :





@Etienne How's Edit_multiple page name is actually named ? Wonder, how getting without get method the Edit_multiple page.
– 7urkm3n
Jun 30 at 9:57


Edit_multiple


Edit_multiple





@Etienne can you update the error message and stack trace in question after the change i suggested?
– Jagdeep Singh
Jun 30 at 10:12





Why are there two before_action :authenticate_user!s in your controller?
– Jagdeep Singh
Jun 30 at 10:13


before_action :authenticate_user!









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

List of Kim Possible characters

Python Tkinter Error, “Too Early to Create Image”