无法传递params数据来创建用户

时间:2013-05-28 03:48:30

标签: json ruby-on-rails-3 get

我有一个rails应用程序接受json请求来创建用户。一个sign_up基本上是一个GET。当我传入此网址http://localhost:3000/sign_up.json?username=chalam&email=holy@yahoo.com&name=chalami&password=chalami时,我无法创建用户 我知道我在公开时传递了密码,但这不是我关心的问题。我检查了日志,这就是我所看到的

Started GET "/sign_up.json?username=chalam&email=holy@yahoo.com&name=chalami&password=[FILTERED]" for 127.0.0.1 at 2013-05-28 10:37:37 +0700
Processing by UsersController#create as JSON
Parameters: {"username"=>"chalam", "email"=>"holy@yahoo.com", "name"=>"chalami", "password"=>"[FILTERED]"}
(0.1ms)  begin transaction
User Exists (0.1ms)  SELECT 1 AS one FROM "users" WHERE "users"."username" IS NULL LIMIT 1
User Exists (0.0ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" IS NULL LIMIT 1
(0.0ms)  rollback transaction
(0.0ms)  begin transaction
CACHE (0.0ms)  SELECT 1 AS one FROM "users" WHERE "users"."username" IS NULL LIMIT 1
CACHE (0.0ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" IS NULL LIMIT 1
(0.0ms)  rollback transaction
Completed 200 OK in 6ms (Views: 0.1ms | ActiveRecord: 0.3ms)

正如您所看到的,尽管参数Parameters: {"username"=>"chalam", "email"=>"holy@yahoo.com", "name"=>"chalami", "password"=>"[FILTERED]"}清楚地表明参数正在传递,但它们并未输入数据库CACHE (0.0ms) SELECT 1 AS one FROM "users" WHERE "users"."username" IS NULL LIMIT 1 CACHE (0.0ms) SELECT 1 AS one FROM "users" WHERE "users"."email" IS NULL LIMIT 1  主要问题是来自params的数据未被“用于”创建用户

有一点需要注意,我可以通过终端中的控制台创建用户。我将尝试包括所有相关信息 rails版本:3.2.13和 我使用的是瘦而不是WEBBRICK

我的user.rb

class User < ActiveRecord::Base
  attr_accessible :email, :name, :username
  attr_accessor :password
  before_save :encrypt_password

  validates_uniqueness_of :username, :email
  validates_presence_of :password, :on => :create
  validates_presence_of :email, :username

  def encrypt_password
  if password.present?
    self.password_salt = BCrypt::Engine.generate_salt
    self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
  end
  end

  def self.authenticate(username, password)
user = find_by_username(username)
if(user && user.password_hash == BCrypt::Engine.hash_secret(password,  user.password_salt))
    user
else
    nil
end
  end
end

我的控制器

class UsersController < ApplicationController
respond_to :json
    def new
 @user = User.new
    end

    def create
  @user = User.new(params[:user])
  @user.save
  if @user.save
    respond_to do |format|
        format.json { render :json => {:status => "200", :message => "Signed up successfully"}}
    end
  else
    respond_to do |format|
         puts @user.errors.inspect 
        format.json { render :json => {:status => "400", :message => "Failed"}}
    end
  end
  end

  def index
@user = User.all
render :json =>@user
  end
end

1 个答案:

答案 0 :(得分:0)

好的,所以我想出了一个解决方案,我想我应该发布它,它可能有助于人们

问题:我无法使用Json响应创建用户,尽管我可以通过rails console

来完成

错误:

  1. 我创建了一个GET路由来创建一个危及安全性的用户
  2. 我将密码字段添加到attr_accessor,而不是attr_accessible
  3. 解决方案:

    1. 将操作从GET更改为POST(在routes.rb中)。要创建用户(对于POST),我使用了curl。就我而言,我的curl声明是 curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d ' {"user":{"name":"name","username":"username","email":"email@email.com","password":"app123"}}' http://localhost:3000/sign_up。这是一篇关于curl
    2. 的有用帖子

      2.在我的用户模型中,我添加了attr_accessor :password以及attr_accessible :password