web-dev-qa-db-ja.com

Ruby on Rails

私の開発環境とテスト環境では、たくさんのユーザーをデータベースにシードしたいと考えています。 Ruby on Rails v3.2.8および最新のDeviseを使用しています。そのため、db/seeds.rbファイルに次の行を追加しました:

User.create(email: '[email protected]', encrypted_password: '#$taawktljasktlw4aaglj')

しかし、rake db:setupを実行すると、次のエラーが発生します。

熊手は打ち切られました!保護された属性を一括割り当てできません:encrypted_pa​​ssword

ユーザーをシードする適切な方法は何ですか?

28
at.

あなたはこのようにする必要があります:

user = User.new
user.email = '[email protected]'
user.encrypted_password = '#$taawktljasktlw4aaglj'
user.save!

このガイドを読んで、一括割り当てとは何かを理解してください: http://guides.rubyonrails.org/security.html

なぜ暗号化されたパスワードを直接設定する必要があるのでしょうか。あなたはこれを行うことができます:

user.password = 'valid_password'
user.password_confirmation = 'valid_password'
38

アルンは正しいです。あなたのseeds.rbでこれを行うだけの方が簡単です

user = User.create! :name => 'John Doe', :email => '[email protected]', :password => 'topsecret', :password_confirmation => 'topsecret'
25
George

確認のスキップ方法は、ユーザーモデルに適合モジュールがある場合にのみ機能し、それ以外の場合は削除します

  user = User.new(
      :email                 => "[email protected]",
      :password              => "123456",
      :password_confirmation => "123456"
  )
  user.skip_confirmation!
  user.save!

これは古い質問ですが、これは管理者ユーザー(cancancanのユーザー)の例です。

User.create!([
  {email: "[email protected]", password: "testadminuser", password_confirmation: "testadminuser", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2015-02-06 14:02:10", last_sign_in_at: "2015-02-06 14:02:10", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", admin: true},
  {email: "[email protected]", password: "testuseraccount", password_confirmation: "testuseraccount", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2015-02-06 14:03:01", last_sign_in_at: "2015-02-06 14:03:01", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", admin: false},
  {email: "[email protected]", password: "testcustomeruser", password_confirmation: "testcustomeruser", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2015-02-06 14:03:44", last_sign_in_at: "2015-02-06 14:03:44", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", admin: false}
])
8
Koxzi

Devise confirmableモジュールを使用している場合は、次のようにする必要があります。

user = User.new(
  email: '[email protected]', 
  password: '123456789', 
  password_confirmation: '123456789'
)
user.skip_confirmation!
user.save!

skip_confirmation!呼び出すだけで、このアカウントを確認する必要がないことを考案します。
他のオプションはconfirmed_atとしてのユーザー属性Time.now.utc保存前。

5
Victor

それが役立つかどうかはわかりませんが、実際にこれを行ってデフォルトの管理者ユーザーRails 5アプリですが、GitHubリポジトリでプレーンテキストのパスワードを共有していません。

基本的に、これのロジックは次のとおりです。

  • シード時にデフォルトユーザーの安全なランダムパスワードを生成します。
  • ".../admins/sign_in"に移動し、「パスワードをお忘れですか?」リンクでリセットします。
  • そのデフォルトのメールアカウントでreset password linkを取得します。
  • 新しいパスワードを設定します。

したがって、db/seeds.rbこれを追加:

randomPassword = Devise.friendly_token.first(8)
mainAdminUser = Admin.create!(email: "[email protected]", password: randomPassword, name: "Username")

Devise confirmable機能を使用している場合は、次のようにして確認可能なオプションをスキップしてください:

mainAdminUser = Admin.new(
    email: "[email protected]",
    password: randomPassword,
    password_confirmation: randomPassword,
    name: "Username"
)
mainAdminUser.skip_confirmation!
mainAdminUser.save!

よろしくお願いします。

これでデフォルトのユーザーができましたが、デフォルトのパスワードを共有していません!私はそれが誰かのために役立つことを願っています。

4
Alex Ventura

Usersテーブルをシードするには:

User.create(
        email: "[email protected]",
        password: "12345678"
    )

devise がインストールされている場合、:passwordは自動的にハッシュされ、:encrypted_passwordに保存されます

4
doncadavona

Seeds.rbファイルのデバイスユーザーの場合、私にとってうまくいったのは、新しいユーザーを保存するときに汎用パスワードを使用することでした。次に、暗号化されたパスワードを更新して、モデルを再度保存します。これはハックな方法でした。

user = User.new(
    :email                 => "[email protected]",
    :password              => "fat12345",
    :password_confirmation => "fat12345"
)
user.save!
user.encrypted_password="ENCRYPT.MY.ASS!!!KJASOPJ090923ULXCIULSH.IXJ!S920"
user.save

他の人が投稿したUPDATEはこれを行うためのより良い方法です:

user = User.new(
    email: "[email protected]", 
    password: "foob1234", 
    password_confirmation: "foob1234"
)
user.skip_confirmation! #only if using confirmable in devise settings in user model.
user.save!
1
lacostenycoder

Db/seeds.rbファイルを使用して、最初のユーザーを開始します。

User.create!(email: '[email protected]', 
             password: '123456789', 
             password_confirmation: '123456789')
1
aloucas

私の要件の1つで同じことをしたので、スニペットを貼り付けるだけ

def triggerSeedUsers
      p "Starting Seeding Users..."
      p   "Deleting all users"..
      User.destroy_all
      normal_users = [{:email => '[email protected]', :login => "abc_demo", :name => 'abc Demo'}]
      admin_users = [{:email => '[email protected]', :login => 'abc_admin', :name => 'abc Admin'}]

      [normal_users,admin_users].each do |user_type|
        user_type.each do |user|
          User.create!(:name => user[:name],
            :login => user[:login],
            :email => user[:email],
            :first_login => false,
            :password => 'P@ssw0rd',
            :password_confirmation => 'P@ssw0rd'
            )
        end
      end
      User.where('name LIKE ?', '%demo%').update_all(:is_admin => 0)
      User.where('name LIKE ?', '%admin%').update_all(:is_admin => 1)
     end
0
AnkitG

db:seedで確認する電子メール:

User.create!( name: 'John', email:'[email protected]', password: '123456', password_confirmation: '123456',confirmed_at: '2018-08-04 04:51:43', current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1")
0
vidur punj

これにより、エラーなしでシードを複数回実行できます。

User.where(email: "[email protected]").first_or_create.update_attributes(nome: "Your Name",
    email: "[email protected]",
    password:              "password#123",
    password_confirmation: "password#123")

:password属性のrestを追加するだけで、deviseがpassword_encryptを実行します

user = User.new({ email: '[email protected]', password: 'EnterYourPassword'})
user.save!
flash[:notice] = 'User Created'

#or for extra logic

        #if user.save
          #ExtraCredentialsOrLogic

        #elsif user.errors.any?
          #user.errors.full_messages.each do |msg|
            #puts msg
          #end
        #else
          #puts "****NOT VALID****"
    #end

そして今「rake db:seed」を実行します

0
Bilal A.Awan