Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.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_Ruby_Ruby On Rails 3_Ruby On Rails 3.2 - Fatal编程技术网

Ruby on rails rails删除具有多个条件的记录

Ruby on rails rails删除具有多个条件的记录,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-3.2,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 3.2,我正在尝试使用.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

我正在尝试使用
.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" %>
我没有得到
puser\u id

“某物”或要删除的对象)**,方法::删除,:类=>“纯按钮纯按钮主”%>
 <%= 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放入链接。

尝试以下操作:

<%= link_to "Delete me", object_item_destroy_path(object_id: @object), method: :delete, :class => "pure-button pure-button-primary" %>
在这里,当你说:
Mine.where(:puser\u id=>@puser.where(:object\u id=>@object)
时,它会给你一个
ActiveRecord::Relation
对象,而不是
Mine的
对象,因此
destroy
方法无法用你试图调用它的方式使用

我也鼓励你在这里使用联想。比如:

@puser.mines.where(:object_id => @object).first
# or
@puser.mines.find_by_object_id(@object)

好的,我得到了我的参数,但是我仍然得到了错误数量的参数(0代表1)errorOk,你能找出哪个方法没有得到它的参数吗?你能发布你得到的错误跟踪吗?比如你得到的是:
错误的参数数(0代表1)
、行号、文件等?@Surya错误在这一行:
@mine.destroy
,但下面是错误的文本:
app/controllers/object\u controller.rb:126:在“item\u destroy”中检查更新的答案。:)
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
@puser.mines.where(:object_id => @object).first
# or
@puser.mines.find_by_object_id(@object)