活动记录在保存用户时失败

时间:2015-02-02 01:34:38

标签: ruby validation activerecord auto-increment camping

我正在尝试在Camping应用程序上实现身份验证系统,我对User模型进行了重大更改以合并它,但现在我的post方法不会自动分配和auto_incrememnt user_ids到我的User类。

我正在尝试使用这些教程: http://www.sitepoint.com/rails-userpassword-authentication-from-scratch-part-i/ http://www.sitepoint.com/rails-userpassword-authentication-from-scratch-part-ii/

我目前不使用视图,我发送HTTParty帖子来创建用户,但响应返回时id为null且记录未保存到DB:

resp = "{\"id\":null,\"username\":\"testusername\",\"email\":null,\"encrypted_password\":null,\"salt\":null,\"password\":\"testpassword\",\"name\":\"testname\",\"github_login\":\"testgithub\",\"created_at\":null,\"updated_at\":null}"

任何人都可以帮助解决这个问题或让我走上正轨吗?谢谢!

(抱歉发布了这么多代码,我是新手记录/露营的新手,不知道我的错误可能在哪里。)

继承我的模特:

module Notepasser
  include Camping::Session
end

module Notepasser::Models

  class User < ActiveRecord::Base
    EMAIL_REGEX = /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
    include BCrypt
    attr_accessor :password, :password_confirmation

    has_many :sent_messages, :class_name => 'Message', :foreign_key => 'sender_id'
    has_many :received_messages, :class_name => 'Message', :foreign_key => 'recipient_id'  
    validates :username, 
              :presence => true,
              :uniqueness => true, 
              :length => { :in => 3..20 }
    validates :email, 
              :presence => true, 
              :uniqueness => true, 
              :format => EMAIL_REGEX
    validates :password, :confirmation => true
    validates_length_of :password, :in => 6..20, :on => :create
    before_save :encrypt_password
    after_save :clear_password

    def encrypt_password
      if !password.empty?
        self.salt = BCrypt::Engine.generate_salt
        self.encrypted_password= BCrypt::Engine.hash_secret(password, salt)
      end
    end

    def clear_password
      self.password = nil
    end

    def self.authenticate(username_or_email="", login_password="")
      if  EMAIL_REGEX.match(username_or_email)    
        user = User.find_by_email(username_or_email)
      else
        user = User.find_by_username(username_or_email)
      end
      if user && user.match_password(login_password)
        return user
      else
        return false
      end
    end

    def match_password(login_password="")
      encrypted_password == BCrypt::Engine.hash_secret(login_password, salt)
    end
  end

  class Message < ActiveRecord::Base
    belongs_to :sender, :class_name => 'User'
    belongs_to :recipient, :class_name => 'User'
  end
end

这是我遇到问题的控制器,user_id没有自动生成,当pry进入post并调用@ current_user.save时,它返回false,这让我觉得我在User模型上验证失败但是我只是不知道在哪里。

module Notepasser::Controllers
  class Users < R '/users'
    attr_reader :current_user

    #new user
    def post
      username ||= @input.username
      password ||= @input.password
      name     ||= @input.name
      github   ||= @input.github_login
      @current_user = Notepasser::Models::User.new(username: username, 
                                                   password: password, 
                                                   name: name,
                                                   github_login: github) 
      @current_user ? @status = 200 : @status = 400
      @current_user.save
      @current_user.to_json
    end
  end
end

0 个答案:

没有答案