我的问题和他的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
endIn 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现在,当我删除多个记录时,出现了新的错误
NoMethodError in ModsController#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"}发布于 2014-03-16 21:26:06
你可以尝试:
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发布于 2014-03-19 13:24:21
事实表明,我的破坏方法是不必要的。当我删除它并注释route.rb中的重复资源方法时,可以解决问题。
# resources :mods
resources :mods do
collection do
delete 'destroy_multiple'
end
endhttps://stackoverflow.com/questions/22443064
复制相似问题