Multi update boolean state with checkboxes (Rails 3)
well this problem I'm sure it isn't difficult at all but being new to rails I'm kinda lost. After watching Railscasts Episode #52, I went on to make my own publish/unpublish list of slides.
So this is the form view
= form_tag publish_admin_category_slides_path(@cat4), :method => "put" do #pricing %table#plans %thead %tr %th Image %th Published %tbody - @image.each do |im| %tr.odd %td = image_tag im.avatar.url(:thumb) = link_to "Delete", admin_category_slide_path(@cat4,im), :method => "delete" %span is %th = check_box_tag "slide_published", im.published ,im.published
With controller action #publish
def publish Slide.update_all(:published => params[:slide_published]) redirect_to :action => "index" end
What would be the correct way to write something like this? A list with checkboxes that changes boolean state of an attribute and multi updates?
Some xp l8r I was able to find something like this. I post it in case someone has a similar problem
def publish @cat = Category.find(params[:category_id]) if params[:slide_published] checked_slides = Slide.find(params[:slide_published]) end unless checked_slides.blank? @cat.slides.all.each do |slide| if checked_slides.include?(slide) slide.update_attribute :published, true unless slide.published? else slide.update_attribute :published, false unless !slide.published? end end end if checked_slides.blank? @cat.slides.all.each do |slide| slide.update_attribute :published, false unless !slide.published? end end