我正在尝试使用.find
或.where
在我的数据库中查找要删除的某条记录,但我遇到了麻烦......
我的路线中有这个
resources :objects do
delete 'item' => 'objects#item_destroy', :as => :item_destroy
end
在我的控制器中
def item_destroy
@object = Object.find(params[:object_id])
@puser = Puser.find_by_user_id(current_user)
if @mine = Mine.where(:puser_id => @puser).where(:object_id => @object)
@mine.destroy
respond_to do |format|
format.html { redirect_to :back }
format.xml { head :ok }
end
end
end
但是我收到了这个错误:
wrong number of arguments (0 for 1)
我破坏的链接是:
<%= link_to "Delete me", object_item_destroy_path, method: :delete, :class => "pure-button pure-button-primary" %>
有人可以帮助我吗?
谢谢!
EDIT ::
另外,当我只获得这些参数时:
{"_method"=>"delete",
"authenticity_token"=>"fdsagfHFUWlfiwoqobGBW92&9",
"object_id"=>"21"}
我没有得到puser_id
?
答案 0 :(得分:1)
试试这个:
<%= link_to "Delete me", object_item_destroy_path(object_id: @object), method: :delete, :class => "pure-button pure-button-primary" %>
<强>更新强>
更改您的objects_controller.rb文件的方法:
def item_destroy
@object = Object.find(params[:object_id])
@puser = Puser.find_by_user_id(current_user)
if @mine = Mine.where(:puser_id => @puser).where(:object_id => @object).first
@mine.destroy
respond_to do |format|
format.html { redirect_to :back }
format.xml { head :ok }
end
end
end
在这里,当您说:Mine.where(:puser_id => @puser).where(:object_id => @object)
时,它会为您提供一个ActiveRecord::Relation
对象,而不是Mine's
对象,因此destroy
方法不可用。我试图调用它。
我也鼓励你在这里使用协会。像:
@puser.mines.where(:object_id => @object).first
# or
@puser.mines.find_by_object_id(@object)
答案 1 :(得分:0)
<%= link_to "Delete me", object_item_destroy_path**(:id => "something" or the object you want to delete)**, method: :delete, :class => "pure-button pure-button-primary" %>
是的,没有传入ID,因为没有ID被放入链接。