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?

Answers


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

Need Your Help

Bookmarklets that work with Frames?

javascript bookmarklet frames frameset

Over the next year, I'll be hacking on an off-the-shelf web app from a Major Software Vendor. While we will have access to large portions of the code base, I'll be working blind on some of the web ...

Line up decimal place when using re.sub to replace decimal numbers of varying size

python regex string

It seems to me that the default behavior for replacement of matched strings with a larger string is to begin replacement at the starting position of the matched string ie.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.