这个问题我确定这并不困难,但对于铁杆我是有点失落的。 在观看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
写这样的东西的正确方法是什么? 带有复选框的列表,用于更改属性的布尔状态和多个更新?
答案 0 :(得分: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