#52 Update through Checkboxes
See how to select multiple items using checkboxes and perform an action on the selected items in this episode.
- Download:
- mp4Full Size H.264 Video (17.8 MB)
- m4vSmaller H.264 Video (11.3 MB)
- webmFull Size VP8 Video (34.4 MB)
- ogvFull Size Theora Video (24.6 MB)
let me see
Another great railscast as usual. One thing that I'd like to know is if there's a way to make the checkboxes each have a different ID in the HTML code, and still have the form work?
I noticed in the example that every checkbox has the same ID, which is invalid, as ID's are meant to be unique within a XHTML document.
Thanks again Ryan for you great work.
@Leeky - It is actually the 'name' of the field that is referenced (and not the id) for submission. It is common practice (as many rails helpers do) to call the id and name the same. Im sure you can used the name as described in this screencast and use the dom_id plugin (http://topfunky.net/svn/plugins/dom_id/) to give your forms unique ids. This would be helpful for validation and for giving this task ajaxy goodness.
Great job Ryan. One thing your code snippet has "form_for" not "form_tag".
If we were to do this with multiple users, how can we first ensure that the task IDs belong to the current logged in user (in case someone edits the HTML and puts in a task ID that doesn't belong to them)?
@Rob
current_user.tasks.update_all(["completed_at=?", Time.now], :id => params[:task_ids])
end
Sorry two other things:
- submit_tag needs a closing %>
- it's ":collection" not ":collections"
:)
@JackVandaL, thanks, fixed. I gotta learn to copy and paste the code instead of retyping it. :/
Ryan,
Great episode. I have question concerning the controller. I see the pattern of always having a new -> create, edit -> update method.. But then I see some repeated code in the new and the create (like typically find the object by ID). Could use call the new method to do the setup of the default data and override them from the create method? Or is there a better way to refactor that?
This was great. Just wondering if there is a way to do this without using REST?
My app is not as clean as this one at this point and I am lost when it comes to getting the "collection" of data passed to the controller method.
I have a question. In this example the items disappear from the list when they are marked as complete.
What if you want to display the complete list all the time, but the check_box default value is based on the values from the tasks.
I am still a little bit confused on how I can give each of the checkboxes a unique id attribute. I'd like to create a label for each of my checkboxes which I am currently unable to do without unique ids.
Any help will be most appreciated.
@skoppy
If you are using fields_for in the way that Ryan is in his code, Rails should provide a unique id. If that's not working, you could always pass your own id as an HTML attribute:
f.check_box :property, :id => "some-unique-id"
Im trying to update Proyect-Users-role taking this approach. But i have an ActiveRecord::ReadOnlyRecord message. My assiciation table is projects_users and it has project_id, user_id, role_id as fields. How can this be done?
ryan, absolutely rate your casts. Best thing since cheese on toast.
Ive tried to create an ajax version of this using form_remote_tag but having problems (im fairly new at rails). Im wondering is implementing ajax functionality for this situation any more complicated than usual ajax stuff and hence why you didnt introduce it here?
Could you provide me any hints or warnings regarding this?
Hello Ryan, I have this solutin in an application, but since I updated to rails 2.3.3 the parameters are note passed as an array, but as a hash like this:
expected
"task_ids"=>["1","2","3","4"]
now I receive =>
"task_ids"=>[{"1"=>nil}, {"2"=>nil}, {"3"=>nil}, {"4"=>nil}]
do you know if it is an issue or the default new behavior should be as is....
thanks
Alexandre, I got this problem some year ago and fixed it somehow. Can u please provide us with your exact view-code and full params at submit...
I'm having the same issue as @Alexandre (Rails 2.3.3)
http://da-crystal.net/GCMS/blog/checkboxlist-in-qt/
Hey Ryan, I know you did this tutorial a long time ago but I'm wondering what are the changes to do this with Rails 3.
(I hope you read this comment!)
Thanks ryan.
It works well.
By
Mohamed
Hi Ryan. I swear I had this working....maybe this is a rails 3.2.3 problem. Im getting a "undefined local variable or method" error on my form action. Everything is set up following your directions.
<%= form_tag require_certificatendorsements_path, :method => :put do %>
Any ideas?
This episode has been updated for Rails 5 as a blog post. Update Through Checkboxes in Rails 5
For some reason, my complete action keeps going into the update action of my controller. Why is this behavior happening, and what can I do to fix it?