wrong number of arguments (0 for 1) while create my user

匿名 (未验证) 提交于 2019-12-03 00:59:01

问题:

I know there is a lot of question about that but I can't find an answer for my case.

So, I build a Rails 4 API, I try to create a User with a post JSON request.

Here some pieces of my code :

user.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 

So I try to send a request with the following params

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

But I get the following error :

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) 

So I'm a little bit desperate about that so if someone have some explenations to clarify me, It would be great. Thank's

EDIT :

My User schema

  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 

I also use this gem https://github.com/gonzalo-bulnes/simple_token_authentication

回答1:

Ok I found the problem, I replaced :

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 

By this :

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 

It's now working



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!