Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 删除或编辑同一rails表单中的多个对象_Ruby On Rails_Forms_Ruby On Rails 3.1_Action - Fatal编程技术网

Ruby on rails 删除或编辑同一rails表单中的多个对象

Ruby on rails 删除或编辑同一rails表单中的多个对象,ruby-on-rails,forms,ruby-on-rails-3.1,action,Ruby On Rails,Forms,Ruby On Rails 3.1,Action,我使用Railscast第198集创建了一个表单,允许我使用复选框分别编辑多个对象。我想能够选择“编辑”或“删除”的行动后,复选框的项目,我想改变。我已将此添加到我的照片\u controller.rb中,用于编辑操作: def edit_individual @photos = Photo.find(params[:photo_ids]) end def update_individual @user = current_user @photos = Photo.up

我使用Railscast第198集创建了一个表单,允许我使用复选框分别编辑多个对象。我想能够选择“编辑”或“删除”的行动后,复选框的项目,我想改变。我已将此添加到我的照片\u controller.rb中,用于编辑操作:

 def edit_individual
  @photos = Photo.find(params[:photo_ids])
 end

  def update_individual
   @user = current_user
   @photos = Photo.update(params[:photos].keys, params[:photos].values).reject { |p|   p.errors.empty? }
  if @photos.empty?
    flash[:notice] = "Products updated"
    redirect_to photos_url
  else
    render :action => "edit_individual"
  end
 end
在我看来,当我循环浏览每张照片以显示它时,我添加了这行代码:

<%= form_tag edit_individual_photos_path, :method => "get" do %>
   ... #loop through all photos and add a checkbox
   <%= check_box_tag "photo_ids[]", photo.id %>
<%= submit_tag "Edit", :class => "btn btn-large btn-inverse" %> 
“get”do%>
... #循环浏览所有照片并添加复选框
“btn btn大btn反向”%>
这很好,但我不知道如何在表单中添加另一个提交标记来删除所选项目,而不仅仅是编辑它们。有人知道我如何将照片ID数组作为参数传递并销毁它们吗?

重复问题


唯一的区别是,在这个问题上,他们使用的是
form_表示
f.submit
s,而不是
form_标记
submit_标记
s,但这应该很容易理解。您按钮的值将是“编辑”和“删除”,而不是“A”和“B”。

在Ashitaka的帮助下,我为控制器中的删除操作想出了这个。编辑/更新是默认操作,因此我只需指定单击的按钮是否为“删除”

在照片_controller.rb中:

def edit_individual
  ...
  if params[:commit] == 'Delete'
    @photos = Photo.find(params[:photo_ids])
    @photos.each { |photo|
      photo.remove_image!
      Photo.destroy(photo.id)  }
    redirect_to photos_new_path
  end
end
他认为:

<%= form_tag edit_individual_photos_path do %>
  ...#loop through all of the photos and add checkbox 
  <%= check_box_tag "photo_ids[]", photo.id %>
   #two submit buttons for the different actions
  <%= submit_tag "Edit", :class => "btn btn-large btn-inverse" %> 
  <%= submit_tag "Delete", :class => "btn btn-large btn-danger" %> 
<% end %>

…#循环浏览所有照片并添加复选框
#两个提交按钮用于不同的操作
“btn btn大btn反向”%>
“btn btn大型btn危险”%>

这告诉我如何区分单击的按钮-谢谢。我仍然无法将照片ID数组传递给销毁操作并将其全部删除。我不确定这是否是因为我指定这是一个get请求,或者是否有其他原因。如果这样做,您的ID将全部位于params[:photo_id]内,并将发送到编辑操作。在编辑操作中,如果参数[:commit]=“Delete”,则必须有一个
,并在该参数中遍历所有ID,找到它们,然后销毁找到的对象。酷-谢谢。我能弄明白。将使用我最终使用的代码添加答案