【问题标题】:wrong number of arguments (0 for 1) while create my user创建我的用户时参数数量错误(0 表示 1)
【发布时间】:2014-11-30 12:37:09
【问题描述】:

我知道对此有很多问题,但我找不到我的案例的答案。

所以,我构建了一个 Rails 4 API,我尝试使用 post JSON 请求创建一个用户。

这是我的一些代码:

用户.rb:

 class User < ActiveRecord::Base
  acts_as_token_authenticatable
  has_secure_password

  belongs_to :current_position, :class_name => "Position", foreign_key: "position_current_id"
  has_many :positions
  searchkick locations: ["location"]

  def search_data
    attributes.merge location: [current_position.latitude, current_position.longitude]
  end

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

application_controller.rb

class ApplicationController < ActionController::Base
  respond_to :json, :html
  protect_from_forgery with: :null_session,
                         if: Proc.new { |c| c.request.format =~ %r{application/json} }
end

registrations_controller.rb

class Api::RegistrationsController < ApplicationController

  respond_to :json

  def create
    user = User.new(user_params)
    if user.save
      render :json => {:token => user.authentication_token, :email =>user.email}, :status=>201
      return
    else
      warden.custom_failure!
      render :json =>user.errors, :status=>422
    end
  end

  def user_params
    params.require(:user).permit(:email, :password, :password_confirmation, :username)
  end
end

所以我尝试使用以下参数发送请求

{"user":{"email":"user5@gmail.com", "password" : "123456789", "password_confirmation" : "123456789", "username" : "toto"}}

但我收到以下错误:

started POST "/signup.json" for 127.0.0.1 at 2014-11-30 13:27:02 +0100
Processing by Api::RegistrationsController#create as JSON
  Parameters: {"user"=>{"email"=>"user5@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "username"=>"toto"}, "registration"=>{"user"=>{"email"=>"user5@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "username"=>"toto"}}}
   (0.1ms)  begin transaction
   (0.1ms)  rollback transaction
Completed 500 Internal Server Error in 95ms

ArgumentError (wrong number of arguments (0 for 1)):
  app/controllers/api/registrations_controller.rb:19:in `create'


  Rendered /Users/billey_b/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.5ms)
  Rendered /Users/billey_b/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.2ms)
  Rendered /Users/billey_b/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
  Rendered /Users/billey_b/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (17.9ms)

所以我对此有点绝望,所以如果有人有一些解释来澄清我,那就太好了。谢谢

编辑:

我的用户架构

  create_table "users", force: true do |t|
    t.string   "email",                default: "", null: false
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "firstname"
    t.string   "lastname"
    t.integer  "age"
    t.string   "gender"
    t.string   "avatar"
    t.datetime "birth"
    t.string   "status"
    t.string   "username"
    t.string   "password_digest"
    t.string   "encrypted_password"
    t.string   "authentication_token"
    t.integer  "position_current_id"
  end

我也用这个宝石https://github.com/gonzalo-bulnes/simple_token_authentication

【问题讨论】:

  • 不应该是"user = User.new(user_params)" --> "user = User.new(params[:user])" 吗?
  • 注册控制器的第 19 行是什么?
  • @epergo 如果我执行你的方法,我会得到 ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError): app/controllers/api/registrations_controller.rb:18:in `create' @dax 第 19 行是用户.保存
  • 您的用户模型有哪些属性? Some attribute names are not allowed.
  • 我更新了我的问题以添加用户属性

标签: ruby-on-rails rest ruby-on-rails-4 ruby-on-rails-4.1


【解决方案1】:

好的,我找到了问题,我换了:

class User < ActiveRecord::Base
  acts_as_token_authenticatable
  has_secure_password

  belongs_to :current_position, :class_name => "Position", foreign_key: "position_current_id"
  has_many :positions
  searchkick locations: ["location"]

  def search_data
    attributes.merge location: [current_position.latitude, current_position.longitude]
  end

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

通过这个:

class User < ActiveRecord::Base
  acts_as_token_authenticatable
  has_secure_password

  belongs_to :current_position, :class_name => "Position", foreign_key: "position_current_id"
  has_many :positions
  searchkick locations: ["location"]

  def search_data
    attributes.merge location: [current_position.latitude, current_position.longitude]
  end
end

现在可以了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    相关资源
    最近更新 更多