我的问题与他的问题相似 Rails 3 destroy multiple record through check boxes
它显示了我的错误
Couldn't find Ticket with id=destroy_multiple
所以我改变了我的代码
在 routes.rb
中 resources :tickets do
collection do
delete 'destroy_multiple'
end
end
在mod.html.erb
<%= form_tag destroy_multiple_mods_path, method: :delete do %>
<div class="CSSTableGenerator" >
<table >
<tr>
<td>Delete</td>
<td>Edit</td>
<td>Detail</td>
<td>Material_Code</td>
<td>Material_Name</td>
<td>Material_Type</td>
<td>Unit</td>
<td>Storage_Lowerlimit</td>
<td>Storage_Upperlimit</td>
<td>Material_Unit_price</td>
<td>Material_Balance</td>
<td>Material_Total_value</td>
<td>Material_Producer</td>
<td>Material_Location</td>
</tr>
<% @mods.each do |mods| %>
<tr>
<td><%= check_box_tag "mods_ids[]", mods.id %></td>
<td><%= link_to "edit", edit_mod_path(mods.id) %></td>
<td><%= link_to 'detail', mod_path(mods.id)%></td>
<td><%=mods.code %></td>
<td><%=mods.name %></td>
<td><%=mods.version %></td>
<td><%=mods.unit %></td>
<td><%=mods.lowerlimit %></td>
<td><%=mods.upperlimit %></td>
<td><%=mods.unitprice%></td>
<td><%=mods.totality %></td>
<td><%=mods.totalprice %></td>
<td><%=mods.producer %></td>
<td><%=mods.storage %></td>
</tr>
<% end %>
</table>
</div>
<%= submit_tag "Delete selected" %>
<% end %>
在控制器
中def destroy_multiple
Mod.destroy(array_of_ids)
respond_to do |format|
format.html { redirect_to mods_path }
format.json { head :no_content }
end
end
在模型
中class Mod < ActiveRecord::Base
acts_as_xlsx
attr_accessible :name ,:code, :lowerlimit, :producer, :storage, :totality, :totalprice, :version, :unit, :unitprice, :upperlimit
has_many :feedbacks, dependent: :destroy
has_and_belongs_to_many :products
validates :upperlimit, presence: true
validates :lowerlimit, presence: true
validates :name, presence: true
validates :code, presence: true
validates :code, presence: true, uniqueness: { case_sensitive: false }
end
def destroy
Mod.find(params[:mods_ids]).destroy
flash[:success] = "Material destroyed."
redirect_to mods_url
end
现在当我删除多条记录时,我遇到了新的错误
ModsController中的NoMethodError#destroy
undefined method `destroy' for #<Array:0x2bf8678>
我接受了市场的建议 这是第二个错误的参数
Parameters:
{"utf8"=>"✓",
"_method"=>"delete",
"authenticity_token"=>"Z8awZYzgdGXK5eptpe1Erxow3yBGqQU9r+8j2NW4L5M=",
"mods_ids"=>["14",
"15"],
"commit"=>"Delete selected",
"id"=>"destroy_multiple"}
答案 0 :(得分:1)
您可以尝试:
def destroy_multiple
Mod.destroy_all(id: params[:mods_ids])
respond_to do |format|
format.html { redirect_to mods_path }
format.json { head :no_content }
end
end
它类似于:
Mod.where(id: params[:mods_ids]).destroy_all
不要忘记定义正确的路线(注意:mods
而不是:tickets
):
resources :mods do
collection do
delete 'destroy_multiple'
end
end
答案 1 :(得分:0)
事实证明我的破坏方法是不必要的。当我删除它并在route.rb中注释重复资源方法时,可以解决问题。
# resources :mods
resources :mods do
collection do
delete 'destroy_multiple'
end
end