I’m trying to update one field of multiple records in a model, Lead. Each lead is displayed in a table row, and the field being modified for the multiple rows is “selected”, which is a boolean, stored as tinyint 1 or 0, true or false.
leads_controller.rb
def list
@leads = Lead.where({:store_number => session[:store_number]}).order("selected DESC")
@targeted_leads = Lead.where(:store_number => session[:store_number], :selected => true)
end
def update_multiple
@leads = Lead.find(params[:lead_ids])
@leads.update(params[:lead_ids])
redirect_to(:action => 'list')
end
list.html.erb
<%= form_tag update_multiple_leads_path, :method => :put do %>
<table>
<thead>
<tr>
<th>Lead Name</th>
<th>Selected Leads</th>
</tr>
</thead>
<tbody>
<% for lead in @leads %>
<tr>
</td>
<td><%= lead.company_name %></td>
<td>
<%= fields_for("lead_ids[]", lead) do |f| %>
<%= f.check_box("selected") %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= submit_tag('Save Targeted Leads', :class => "btn") %>
<% end %>
Now, what I want this form to do is pass all leads through, and if checked, update that record’s “selected” field to 1, and if unchecked, update the field to 0.
When it passes along, here are the results on the update_multiple action:
ArgumentError in LeadsController#update_multiple
Unknown key: 6
Rails.root: (my/rails/root)
Application Trace | Framework Trace | Full Trace
app/controllers/leads_controller.rb:73:in `update_multiple'
Request
Parameters:
{"commit"=>"Save Targeted Leads",
"lead_ids"=>{"6"=>{"selected"=>"1"},
"1"=>{"selected"=>"0"},
"3"=>{"selected"=>"0"}},
"authenticity_token"=>"ALYtIkJNsAANsHXaAvgeb84G9Ms7OPSkyFO6jkM19yU=",
"_method"=>"put",
"utf8"=>"\342\234\223"}
FYI: (Line 73 references this row in my controller: @leads = Lead.find(params[:lead_ids]) )
When I look at this, it looks like it’s passing everything along, just as I’d want it to… Lead Model, take id 6, set selected to 1. Now take id 1, set selected to 0. What’s really confusing me is that “Unknown key” bit.
My initial thoughts are, it’s not looking for the right field to modify… as in, it knows I’m looking for “6”, but what about “6”? “6” is not a key. Id 6, or something else?
Has anyone else solved this? Thank you in advance for your thoughts.
Figured it out! Took me days (I’m pretty new to ruby), but I got something to work. This will only work in this particular case, because it’s not flexible… Rails probably has a better way to do this, but for others that are stuggling:
Of note, in regards to a previous comment, it’s irrelevant (at least for my purposes) that the id comes through as a string, so no need to hash… but if you wanted to do that, I wrote this while debugging.
Then pass the hash in to the each loop, so…
It’s manually looping through each param and assigning based on the index. So for each id (for example, 1), it’s taking the first index of that id’s array, and manually assigning it to the second index of the same array.
Again, not very Rails-like, and definitely not THE way to do it, but it works. Anyone with a better way, feel free to contribute.