#165 Edit Multiple (revised)
Feb 25, 2013 | 15 minutes |
Forms
Editing one record at a time can be tedious work. Here you will learn three different ways to edit multiple records at once using checkboxes.
- Download:
- source codeProject Files in Zip (130 KB)
- mp4Full Size H.264 Video (34.4 MB)
- m4vSmaller H.264 Video (18.1 MB)
- webmFull Size VP8 Video (23.3 MB)
- ogvFull Size Theora Video (42.5 MB)
Why did he make it a "collection type route" in the first rewrite of the edits (at 1:52 of the video)?
i.e. did he have to do it that way? What would be alternative ways to do it? why is this way better? Thanks if you can help.
Since he's editing multiple records, the operation is on the products collection, or at least a subset thereof.
It also means the url helper (discontinue_products_path) will have the plural form which seems appropriate. YMMV.
resources :products do
put :discontinue,:on => :collection
end
Thanks it works !
Yeah thanks
ou're lucky not at my house, nothing that works
Nice, very usefull
Agree or disagree.
Putting an edit form on an index page (as he does in the video) kind of makes Rails work like a javascript single page app. It seems to load and present the updates much faster, in addition to not having to redirect to a different page
Mike, see my question below...I am doing a similar thing and I definitely get a clear flash when the page is reloaded or re-rendered...
I suspect he's using Turbolinks which is explained in an earlier railscasts.
I highly recommend it as well. It is stupidly easy to implement and will be adopted in rails 4.0 if I recall correctly.
Just to clarify Turbolinks will be included by default in a new Gemfile generated with Rails 4 it is not built into the framework
+1
Great episode, again :)
Oh by the way, is it my imagination or is there absolutely no flashing when you click "discontinue checked" in solution 1? I always get flashing when I do this stuff - unless I am using ajax-y logic. Anyone?
In Rails 4, this:
should be:
+1
This also works (IMO is preferred) in rails 3.2
+1
+1
Thank you!
thanks - +1
Nice +1
I find the way validation errors are displayed in the third solution a bit odd. The validation error is always the same for each product, isn't it? Might as well display it only once then. Or am I missing something here?
Products could have validations like: price can't be lower than 2.99 if the category is "Games" - or something like that :)
I have a version of this that re-uses the form partial and avoids letting you edit fields that require unique values automatically:
https://github.com/spilth/rails-mass-update-test/blob/master/app/form_builders/mass_form_builder.rb
And
https://github.com/spilth/rails-mass-update-test/blob/master/app/views/books/mass_edit.html.erb
I have a feeling such operations should be refactored into a separate model (e.g. BulkProductsUpdater), this has several advantages:
I totally agree, Igor.
I would have a ProductCollection model (not an AR model, but more of a form object wrapping the collection of Product objects) and a ProductCollectionController. It's much cleaner than forcing collection behaviour into the Product and ProductsController classes.
Can you go into a bit more detail? What does your ProductCollection model and controller look like? How about your routes?
I have the same scenarios with many to many categories. I have found difficulty with update method on product. Any ideas to help?
Is there any way to update attributes for miltiple objects with nested form?
I used this code in controller:
It is similar to example
But instead of updating the records, Ruby creates new ones and nullifies "work_id" in existing records.
What could cause this problem? Thank you!
This is great! I was able to take the controller from option 2 and combine it with the wice_grid gem to quickly put together a grid featuring inline editing without any client-side script at all. As a newbie to Rails, I expected to be fiddling around for hours. Easy-peasy!
in Rails 4.2 this code gives me error
undefined method `reject' for nil:NilClass
product.update_attributes!(params[:product_params].reject { |k,v| v.blank? })
The code for this episode has been updated to Rails 5, download the code form emu
ArgumentError in ProductsController#discontinue
wrong number of arguments (2 for 1)
I'm getting an error here. anybody know how to fix that. It happens in the products controller, in the discontinue action (which we custom created), on this line:
Product.update_all({discontinued: true}, {id: params[:product_ids]})
I'm getting "Couldn't find Product with 'id'=edit_multiple"
anyone know how to make this work in rails 4?
I must have fat fingered it or something...
--EDIT--
OK It was a fat finger.
Now I'm getting "ActiveModel::ForbiddenAttributesError"
Very interesting episode, thank you for it. The community seems a bit old but hey - there are still many interesting things to be found here.
Thanks for this