#165
Jun 08, 2009

Edit Multiple

Use checkboxes to select multiple records and edit them all in one form as shown in this episode. With virtual attributes you can even edit values relatively!
Download (27.7 MB, 14:26)
alternative download for iPod & Apple TV (17.7 MB, 14:26)

Resources

# routes.rb
map.resources :products, :collection => { :edit_multiple => :post, :update_multiple => :put }

# products_controller.rb
def edit_multiple
  @products = Product.find(params[:product_ids])
end

def update_multiple
  @products = Product.find(params[:product_ids])
  @products.each do |product|
    product.update_attributes!(params[:product].reject { |k,v| v.blank? })
  end
  flash[:notice] = "Updated products!"
  redirect_to products_path
end

# models/product.rb
def price_modification
  price
end

def price_modification=(new_price)
  if new_price.to_s.ends_with? "%"
    self.price += (price * (new_price.to_f/100)).round(2)
  else
    self.price = new_price
  end
end
<!-- products/index.html.erb -->
<% form_tag edit_multiple_products_path do %>
<table>
  <tr>
    <th></th>
    <th>Name</th>
    <th>Category</th>
    <th>Price</th>
  </tr>
<% for product in @products %>
  <tr>
    <td><%= check_box_tag "product_ids[]", product.id %></td>
    <td><%=h product.name %></td>
    <td><%=h product.category.name %></td>
    <td><%= number_to_currency product.price %></td>
    <td><%= link_to "Edit", edit_product_path(product) %></td>
    <td><%= link_to "Destroy", product, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>
</table>
<%= submit_tag "Edit Checked" %>
<% end %>

<!-- products/edit_multiple.html.erb -->
<% form_for :product, :url => update_multiple_products_path, :html => { :method => :put } do |f| %>
  <ul>
    <% for product in @products %>
      <li>
        <%= hidden_field_tag "product_ids[]", product.id %>
        <%=h product.name %>
      </li>
    <% end %>
  </ul>
  <p>
    <%= f.label :category_id %><br />
    <%= f.collection_select :category_id, Category.all, :id, :name, :include_blank => true %>
  </p>
  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <p>
    <%= f.label :price_modification %><br />
    <%= f.text_field :price_modification %>
  </p>
  <p>
    <%= f.label :discontinued %><br />
    <%= f.select :discontinued, [["Yes", true], ["No", false]], :include_blank => true %>
  </p>
  <p><%= f.submit "Submit" %></p>
<% end %>

RSS Feed for Episode Comments 28 comments

1. Ryan Jun 08, 2009 at 00:05

Another great screencast! This is the first thing i do on Mondays. Love it!


2. Tex Jun 08, 2009 at 00:11

Many thanks Ryan, great screencast as always !!!


3. Richard Jun 08, 2009 at 01:05

Hi Ryan,

It seems that there's a problem with the iPod version ?

The requested URL /ipod_videos/165_edit_multiple.m4v was not found on this server.

Anyway great work as ever !

Bests,


4. Bob Jun 08, 2009 at 02:05

Hi!

I agree with Richard, I have a 404 on the ipod version?

Regards,


5. Brian Jun 08, 2009 at 04:39

Thanks for making Monday mornings not suck.


6. Ahmed Al Hafoudh Jun 08, 2009 at 05:19

Total awesomeness!!!!


7. Jamie Jun 08, 2009 at 06:47

I really look forward to your Monday morning Railscasts. Thanks again Ryan. I can't wait to watch this on my break this morning.


8. Kai Janson Jun 08, 2009 at 06:55

Can't use the download link nor iTunes to download this (fantastic looking) podcast.

Please fix it. :)

We need you and your podcasts!


9. Ryan Bates Jun 08, 2009 at 07:13

Sorry about that guys, forgot to upload the iPod version. I will do it shortly.


10. Nickmenow Jun 08, 2009 at 07:14

I too am unable to view the Railscast via download link on Safari or save file as or even the iPod link.

As Brian said above "Thanks for making Monday mornings NOT suck" - I second that.


11. Paul Jun 08, 2009 at 08:03

The m4v version is up now.


12. Paul Jun 08, 2009 at 08:04

No it isn't.


13. Ryan Bates Jun 08, 2009 at 08:05

Sorry about the downtime guys. Everything should be up now.


14. Johannes Jun 08, 2009 at 11:37

Cool!

How about a Railscast about "normal" edit forms for several model objects on the same page. Like this one, but allowing you to change multiple product names at the same time.


15. Johan Jun 08, 2009 at 16:27

Such an inspiration


16. Fernan2 Jun 09, 2009 at 01:36

Your lessons are wonderful!!

If I can make a suggest: Wouldn't be better if you make railscasts wide enough to see the code without scrolling? If not the default choice, at least a link to see it at 1024px wide... a lot better than changing it with firebug!!

Thank you


17. Andrea Jun 09, 2009 at 08:47

Thanks Ryan, I love your screencasts!


18. Javier Jun 09, 2009 at 09:19

Nice screencast! Thanks!

Another interesting one would be adding another button to the form to delete the selected records. Since you can't define a form to have two different actions, I've always wondered if that can be done in a RESTful way. Do you just do two different things in the same action?

I guess you already do that with your preview/submit buttons to add a comment ;-).


19. Javier Jun 09, 2009 at 09:48

Oops! Sorry, bad example, and it's already covered in episode 138.

I was referring to a form with two completely different actions and even diferent methods (such as edit and delete).


20. davide Jun 10, 2009 at 04:28

@Javier: i'm not sure im really getting what you mean.. you could make an edit-form, with an additional button_to to your delete action?


21. minikin Jun 10, 2009 at 04:40

thanx man!


22. Javier Jun 10, 2009 at 06:42

@davide: I thought button_to created another form, and HTML doesn't allow one form inside another one. Nor the "button" tag allow you to specify a different action.


23. Erkan Jun 13, 2009 at 18:18

Ryan I have a problem but it's not with this episode I have wall on my page like this but long single string become a problem in view how can I prevent this for example.

:(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((


24. waqas Jun 14, 2009 at 09:30

Hi Ryanb,

First of all another useful screen cast. Would be nice if some AJAX is thrown in there, but looks good as it is. Another thing Ryan, if you wanted to change the name of 3 selected products, hows that done as the form only has one text_field for name and i believe if you enter some name in the text_field that will apply to all three??? can you please shed some light on that?


25. Johan Jun 17, 2009 at 09:47

waqas, wasnt that kind of the point? if you want to change the names individually you can do that easily with the regular form.


26. Artue Jul 04, 2009 at 11:08

First of all another useful screen short.


27. ergun Oct 14, 2009 at 06:30

<a href='http://www.elitdizi.com/' rel='external nofollow'>dizi izle</a>

thank you very much


28. ahmet maranki Oct 23, 2009 at 09:09

thanxx you admin


29. Daresha Feb 24, 2010 at 11:15

I have a problem but it's not with this episode.


30. reis3737 Mar 30, 2010 at 03:13

I wish you continued success thanks nice site


31. Büyü May 12, 2010 at 07:44

china wholesale Computer Peripheral


32. film izle seyret Jun 13, 2010 at 21:41

Often with great pleasure to read your articles and resources would make comments. Let's continue this way as well. Good work.


33. erotik film Jun 13, 2010 at 21:42

Thanks You adminsss


34. Gucci Shop Jul 07, 2010 at 00:09

Your aritcle is very helpful for me,good job!


35. komedi filmleri Jul 30, 2010 at 06:23

Railscast is great.Thanks admin.


36. komedi filmleri Jul 30, 2010 at 06:32

Railscast is great.Thanks admin.


37. free directory list Aug 11, 2010 at 22:36

Ryan, could you show the view that is calling this template?


38. air jordan retro 13 for sale Aug 16, 2010 at 00:36

Thanks for making Monday mornings not suck. Thanks Ryan, I love your screencasts!


39. supra tk society Aug 18, 2010 at 18:54

good job,good article


40. Wholesale baseball hats Aug 20, 2010 at 20:33

Perhaps this is one of the most interesting blogs that I have ever seen. Interesting article, Funny comment. Keep it up!


41. Wholesale Electronics Aug 25, 2010 at 01:55

Discount Wholesale Electronics, Wholesale Cell Phones, Electronic Gadgets and More from the Best Dropship Wholesaler


42. http://www.salepoloshirts.com Aug 26, 2010 at 01:05

are you


43. http://www.oknikesneakers.com Aug 26, 2010 at 01:06

take you


44. louis vuitton shoes Aug 26, 2010 at 21:03

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


45. Smart Cell Phones Aug 30, 2010 at 20:13

thankd for your sharing, i am so happy to read your post.


46. snow boots Aug 30, 2010 at 20:58

Let's continue this way as well. Good work.


47. louis vuitton sunglasses Sep 01, 2010 at 21:18

Good article! Thank you so much for sharing this post.Your views truly open my mind.

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source