7

私はレールを学んでいるところです。Deviseは、認証を迅速かつシームレスに起動して実行するのに優れていることがわかりましたが、1つ質問があります。

Deviseジェネレーター(たとえば、rails g devise User)の最初の実行後にモジュールを変更するにはどうすればよいですか?これは、次の移行でデフォルトになります。

def self.up
  create_table(:users) do |t|
    t.database_authenticatable :null => false
    t.recoverable
    t.rememberable
    t.trackable

    # t.confirmable
    # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
    # t.token_authenticatable

    t.timestamps
  end

  add_index :users, :email,                :unique => true
  add_index :users, :reset_password_token, :unique => true
  # add_index :users, :confirmation_token,   :unique => true
  # add_index :users, :unlock_token,         :unique => true
end

この移行を実行した場合、後の段階でこれらのモジュールの一部を追加/削除するにはどうすればよいですか?たとえば、既存のユーザーモデルにロック可能を追加したい場合があります。モデルに変更を加える方法は理解してdevise.rbいますが、移行をどうすればよいかわかりません。

答えがすでにここにある場合はお詫びします。私はこことグーグルで数時間トロールしましたが、何も見つかりませんでした。多分私は間違ったものを探しています。

前もって感謝します!
ジェイソン
ps。
Rails 3.0.0devise1.1.3
を使用しています

4

4 に答える 4

6

私は同じ質問に対する答えを探していましたが、幸運なことに、その方法を知っている人の隣に座っていました。

以下は、移行スクリプト ( 「rails generate migration add_confirmable_to_users」で生成されたスケルトン スクリプト ファイル) を使用して確認可能なモジュールを含めるようにユーザー モデルを更新する例です。

class AddConfirmableToUser < ActiveRecord::Migration
  def self.up
    change_table :users do |t|
      t.confirmable
    end
    add_index :users, :confirmation_token,   :unique => true
  end

  def self.down
    remove_column :users, :confirmable
    remove_index :users, :confirmation_token
  end
end
于 2010-12-19T01:26:47.393 に答える
2

このエラーが発生していました:

undefined local variable or method `confirmed_at' for #<User:0x000001041531c8> (NameError)

確認可能を追加するには -

移行を生成します。

$ rails generate migration add_confirmable_to_users

移行を編集します。

class AddConfirmableToUsers < ActiveRecord::Migration
  def change
    add_column :users, :confirmation_token, :string
    add_column :users, :confirmed_at, :datetime
    add_column :users, :confirmation_sent_at, :datetime
    add_column :users, :unconfirmed_email, :string
  end
end

http://guides.rubyonrails.org/migrations.html
https://github.com/plataformatec/devise/wiki/How-To:-Upgrade-to-Devise-2.0-migration-schema-style

于 2012-12-09T18:25:21.907 に答える
1

適切なフィールドが既にスキーマに追加されているオプション (確認可能など) を削除するだけである限り、新しい移行を行わずに、いつでも Users モデルを直接編集できます。

于 2010-09-29T00:57:32.747 に答える
0

移行ファイルで必要な行を変更してから、次の手順に従って移行をやり直してください。

http://guides.rubyonrails.org/migrations.html

于 2010-09-29T00:30:02.023 に答える