I’m making a grocery list application in which I’d like to let users check off the items that they’ve placed in the cart. I’d like to do this by toggling the attribute “found” from false to true (within my “item” model).
Here’s the code I have for this:
<% for item in @items %>
<tr>
<td><%= check_box, item, :found, {}, true, false %></td>
<td><%= item.quantity %></td>
<td><%= item.name %></td>
<td><%= item.category %></td>
</tr>
<br />
<% end %>
I then have an “update” link at the bottom of the page. A list has many items, when I ask to update a list, I’m assuming that I’m also updating the attributes of the items within that list.
<%= link_to "Update", @list, :method => :put %> |
Here’s the update action in my list controller:
def update
@list = List.find(params[:id])
if @list.update_attributes(params[:list])
flash[:notice] = "Successfully updated list"
redirect_to @list
else
render :action => 'edit'
end
end
I believe I’m following the documentation for check_box correctly. Nevertheless, somehow the “found” boolean stays false after I update. Does anyone know how to implement this correctly? I’d like to not use AJAX for the time being. Thanks!
Is there a chance that your model might have either attr_accessible on some attributes or attr_protected on :found?
Try to change the update_attributes to update_attributes! and see if you get any more information.
Some other suggestions could be to dump the params to the log, try to do direct assignment to the model (like item.found = params[:found] == ‘1’), make sure that you checkbox 1 or 0 is being correctly interpreted as boolean true, false.