我已經通過railscast 270併爲用戶實現了基本註冊表單(在此應用程序中名爲players
)。表單工作正常,但是當我嘗試通過json進行身份驗證時,出現錯誤。有想法該怎麼解決這個嗎?通過JSON失敗的Rails 3.1身份驗證
POST
請求http://localhost:3000/players
與身體:
{
"email": "[email protected]",
"username": "jaaaack",
"password": "abc123",
"password_confirmation": "abc123"
}
顯示錯誤:
<h2>Form is invalid</h2>
<ul>
<li>Password digest can't be blank</li>
<li>Password can't be blank</li>
</ul>
players_controller.rb
class PlayersController < ApplicationController
def new
@player = Player.new
end
def create
@player = Player.new(params[:player])
if @player.save
redirect_to root_url, :notice => "Signed up!"
else
render "new"
end
end
end
player.rb
class Player < ActiveRecord::Base
attr_accessible :username, :email, :password, :password_confirmation
has_secure_password
validates_presence_of :password, :on => :create
validates_presence_of :email
validates_presence_of :username
validates_uniqueness_of :email
validates_uniqueness_of :username
end
的routes.rb
get "sign_up" => "players#new", :as => "sign_up"
resources :players, :only => [:create]
root :to => 'home#index'
你說得對,創建新的紀錄 - 感謝克里斯。我的json被搗毀了;我決定使用'@player = Player.new(params)'方法。 – Jack
這只是救了我的命 – Kathan