0

好吧,这个问题我敢肯定这根本不难,但是对于 Rails 的新手,我有点迷路了。看完 Railscasts 第 52 集后,我继续制作自己的发布/取消发布幻灯片列表。

所以这是表单视图

    = 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

使用控制器操作 #publish

    def publish

    Slide.update_all(:published => params[:slide_published])
    redirect_to :action => "index"
    end

写这样的东西的正确方法是什么?带有复选框的列表可以更改属性的布尔状态和多次更新?

4

1 回答 1

1

一些 xp l8r 我能找到这样的东西。我发布它以防有人遇到类似问题

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
于 2013-09-09T08:28:10.447 回答