控制器中的ArgumentError

时间:2013-08-24 13:38:19

标签: ruby-on-rails angularjs ruby-on-rails-4

我正在使用Angularjs制作编辑对象表单,Ruby on Rails 4是我的后端。 我收到了以下错误,但没有找到正确的调试方法:

Started PUT "/albums/52109834e9c88c3292000001" for 127.0.0.1 at 2013-08-24 17:24:37 +0400
Overwriting existing field email.
Processing by AlbumsController#update as JSON
Parameters: {"_id"=>{}, "title"=>"Sacred Circuits"}
MOPED: 127.0.0.1:27017 QUERY        database=aggregator_front_development collection=users selector={"$query"=>{"_id"=>"520bd6cbe9c88ca789000001"}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.7932ms)
Completed 500 Internal Server Error in 63ms

ArgumentError (wrong number of arguments (2 for 0..1)):
  app/controllers/albums_controller.rb:18:in `update'

第18行是更新函数,它没有参数。我正在从Angularjs表单发送对象来更新它。 albums_controller.rb:

class AlbumsController < ApplicationController
respond_to :json, :js

def index
    respond_with Album.all
end

def show
    respond_with Album.find(params[:id])

end

def create
    respond_with Album.create(params[:album])
end

def update
    respond_with Album.update(params[:id],params[:album])
end

def destroy
    respond_with Album.destroy(params[:id])
end

private
def album_params
        params.require(:album).permit(:title)
end

end

据我所知,ArgumentError(错误的参数数量(2代表0..1))意味着,但不知道在哪里寻找真正的参数发送。 如何调试这种情况?

1 个答案:

答案 0 :(得分:1)

在更新操作中,update是更新active_record实例的属性的实例方法。它只接受一个论点。但是你在这里传递了2个参数。这就是它产生错误的原因。

更好的方法是先找到专辑记录然后更新。在更新操作中尝试此代码。

.......
def update
  @album = Album.find(params[:id])   #id or whatever key in which you are getting album id
  @album.update(album_params)        #Use strong parameters while doing mass assignment
  ....
end
.......
相关问题