Rails 3.1 通过 json 进行身份验证失败
我已经完成了 railscast 270 并实现了基本注册用户表单(在此应用程序中称为players
)。表单工作正常,但是当我尝试通过 json 进行身份验证时,出现错误。关于如何解决这个问题有什么想法吗?
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.rbplayer.rbroutes.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
POST
向 http
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
://localhost :
get "sign_up" => "players#new", :as => "sign_up"
resources :players, :only => [:create]
root :to => 'home#index'
I've been through railscast 270 and implemented a basic sign up form for users (in this app called players
). The forms work fine, but when I try and authenticate over json, I get errors. Any ideas on how to fix this?
POST
request to http://localhost:3000/players
with body:
{
"email": "[email protected]",
"username": "jaaaack",
"password": "abc123",
"password_confirmation": "abc123"
}
displays errors:
<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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你的问题是由于你的 json 不太正确(以及 Rails 默认解释它的方式)引起的。
如果您检查日志文件 (log/development.log),您将看到类似以下内容的内容(尽管换行符较少):
如您所见,json 数据包含两次 - 一次在 params 哈希的根中,并且再次作为 params 中键的值,使用(我认为)控制器名称作为键。
因此,假设您已经正确设置了 attr_accessible,您可以在 PlayersController#create 中执行以下操作之一:
显然,这会妨碍您在控制器中使用相同的操作来处理来自 HTML 表单提交的数据。为了解决这个问题,您可以按如下方式包装 JSON:
这将使
params[:player]
中包含正确的信息,以便使用现有代码创建新记录I think your problem is being caused by your json not being quite right (and the way rails interprets it by default).
If you inspect your log file (log/development.log) you'll see something like the following (allbeit with fewer line breaks):
As you can see the json data is included twice - once in the root of the params hash, and again as the value for a key in params, using (I think) the controller name as the key.
Thus, given that you've got attr_accessible setup properly for you could do one of the following in PlayersController#create:
Obviously this would hamper your use of the same action in the controller for handling data from a HTML form submission. To get around this you could wrap the JSON as follows:
Which will make
params[:player]
have the right information in it in order to create your new record using your existing code