Modifying devise modules after first generation
I'm in the process of learning rails. I've found Devise to be great with getting authentication up and running quickly and seamlessly but I do have one question.
How do I change the modules after the first run of the Devise generator (e.g. rails g devise User)? This defaults with the following migration:
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 => 开发者_Go百科true
# add_index :users, :confirmation_token, :unique => true
# add_index :users, :unlock_token, :unique => true
end
If I've run this migration, how do I add/remove some of those modules at a later stage? E.g. Maybe I want to add lockable to an existing User model. I understand how to make the changes in the model and devise.rb
but I'm not sure what to do with the migrations.
Apologies if the answer is here already, I've trawled for a couple of hours here and in google and couldn't find anything. Maybe I'm searching for the wrong thing.
Thanks in advance!
Jason ps. I'm using rails 3.0.0 devise 1.1.3I was looking for answer to the same question, and luckily, happened to be sitting next to someone who knew how to do it.
Here is the example of updating the users model to include confirmable module through a migration script (the skeleton script file generated with '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
I was getting this error:
undefined local variable or method `confirmed_at' for #<User:0x000001041531c8> (NameError)
To add Confirmable -
Generate the migration:
$ rails generate migration add_confirmable_to_users
Edit the migration:
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
As long as you're just removing options where the appropriate fields were already added to your schema (like confirmable), you can always just edit the Users model directly without a new migration.
Change the lines you want in the migration file, then redo the migration as per these instructions:
http://guides.rubyonrails.org/migrations.html
精彩评论