当发出GET请求时,Controller提供错误的参数数量错误

时间:2015-11-18 18:39:39

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

我编写了一个包含两个资源usersbooks的rails-api。当我为books发出GET请求时,它正常运行,但对于users,它失败并显示以下消息:

Started GET "/users/" for 127.0.0.1 at 2015-11-18 23:45:52 +0530
Processing by UsersController#index as HTML
  User Load (0.5ms)  SELECT `users`.* FROM `users`
Completed 500 Internal Server Error in 63ms (ActiveRecord: 4.2ms)

ArgumentError (wrong number of arguments (0 for 1)):
  app/controllers/users_controller.rb:8:in `index'


  Rendered /Users/me/.rvm/gems/ruby-2.2.3/bundler/gems/rails-35ca78a07c8b/actionpack/lib/action_dispatch/middleware/templates/rescues/_source.erb (17.6ms)
  Rendered /Users/me/.rvm/gems/ruby-2.2.3/bundler/gems/rails-35ca78a07c8b/actionpack/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.1ms)
  Rendered /Users/me/.rvm/gems/ruby-2.2.3/bundler/gems/rails-35ca78a07c8b/actionpack/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.2ms)
  Rendered /Users/me/.rvm/gems/ruby-2.2.3/bundler/gems/rails-35ca78a07c8b/actionpack/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (69.4ms)

以下是我在浏览器上遇到的错误:

UsersController中的ArgumentError #index

错误的参数数量(0表示1)

提取的来源(第8行):

...       
  # GET /users
  def index        
    @users = User.all

    render json: @users # this is line 8
  end
    ...

配置/ routes.rb中

Rails.application.routes.draw do
  resources :users
  resources :books
end

任何可能出错的线索?

2 个答案:

答案 0 :(得分:2)

错误在于我的模型被传递给序列化程序。我的数据库中有一个名为method的列。 method是一个导轨保留字(读:ruby-and-rails-reserved-words),它正在创建问题。我将我的专栏重命名为registration_method,以解决问题。谢谢大家的帮助:)

答案 1 :(得分:0)

可以尝试渲染@ users.to_json

User.all是否会返回任何用户?