Неверное количество аргументов (0 для 1) при создании моего пользователя
Я знаю, что есть много вопросов по этому поводу, но я не могу найти ответ по своему делу.
Итак, я создаю Rails 4 API, я пытаюсь создать пользователя с пост-JSON-запросом.
Вот несколько фрагментов моего кода:
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
Поэтому я пытаюсь отправить запрос со следующими параметрами
{"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
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
Сейчас работает