Rails 3.1 通过 json 进行身份验证失败

发布于 2024-12-28 19:27:04 字数 1650 浏览 3 评论 0原文

我已经完成了 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

POSThttp

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以往的大感动 2025-01-04 19:27:04

我认为你的问题是由于你的 json 不太正确(以及 Rails 默认解释它的方式)引起的。

如果您检查日志文件 (log/development.log),您将看到类似以下内容的内容(尽管换行符较少):

Processing by PlayersController#create as HTML
Parameters: {
  "email"=>"[email protected]", 
  "username"=>"jaaaack", 
  "password"=>"[FILTERED]", 
  "password_confirmation"=>"[FILTERED]", 
  "players"=>{
    "email"=>"[email protected]", 
    "username"=>"jaaaack", 
    "password"=>"[FILTERED]",
    "password_confirmation"=>"[FILTERED]"
  },
  "controller" => "players",
  "action" => "create"
}

如您所见,json 数据包含两次 - 一次在 params 哈希的根中,并且再次作为 params 中键的值,使用(我认为)控制器名称作为键。

因此,假设您已经正确设置了 attr_accessible,您可以在 PlayersController#create 中执行以下操作之一:

@player = Player.new(params[:players]) 

OR

@player = Player.new(params)

显然,这会妨碍您在控制器中使用相同的操作来处理来自 HTML 表单提交的数据。为了解决这个问题,您可以按如下方式包装 JSON:

{ "player":
  {
    "email": "[email protected]",
    "username": "jaaaack",
    "password": "abc123",
    "password_confirmation": "abc123"
  }
}

这将使 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):

Processing by PlayersController#create as HTML
Parameters: {
  "email"=>"[email protected]", 
  "username"=>"jaaaack", 
  "password"=>"[FILTERED]", 
  "password_confirmation"=>"[FILTERED]", 
  "players"=>{
    "email"=>"[email protected]", 
    "username"=>"jaaaack", 
    "password"=>"[FILTERED]",
    "password_confirmation"=>"[FILTERED]"
  },
  "controller" => "players",
  "action" => "create"
}

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:

@player = Player.new(params[:players]) 

OR

@player = Player.new(params)

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:

{ "player":
  {
    "email": "[email protected]",
    "username": "jaaaack",
    "password": "abc123",
    "password_confirmation": "abc123"
  }
}

Which will make params[:player] have the right information in it in order to create your new record using your existing code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文