2011-10-27 11 views
6

1人のユーザー(管理者)を作成したいと思いますし、コンソールを使用したい(ユーザー登録モデルなし)。私はRailsCast(http://railscasts.com/episodes/270-authentication-in-rails-3-1)のソリューションを使用しています。 しかし私は1つの問題を抱えています。私はUser.create(...、:password => "pass")をコンソールのパスワードにデータベースに保存されたパスワード(passなど)で保存します。私のデータではログインできません。Rails 3.1。

コンソールからユーザーを作成するにはどうすればよいですか? :)

答えて

21

ストレートからRailsのAPI

あなたのハッシュで :password_confirmation => "passを含める必要が
# Schema: User(name:string, password_digest:string) 
class User < ActiveRecord::Base 
    has_secure_password 
end 

user = User.new(:name => "david", :password => "", :password_confirmation => "nomatch") 
user.save              # => false, password required 
user.password = "mUc3m00RsqyRe" 
user.save              # => false, confirmation doesn't match 
user.password_confirmation = "mUc3m00RsqyRe" 
user.save              # => true 
user.authenticate("notright")         # => false 
user.authenticate("mUc3m00RsqyRe")        # => user 

has_secure_passwordをご覧になるには、BCrypt::Password.create(unencrypted_password)を実行してください。上記を行うにはbcrypt-rubyの宝石が必要です。

+0

ありがとうございます!私はBCryptを忘れてしまった、それはパスワードの確認なしでもうまく動作する:) –

+0

恐ろしい、聞いてうれしい=) –