I have a User model created already. I am wondering how should I configure devise with my existing User model. That being said, do I need to setup any additional routes or make atttributes accessible in my user method.
So far user model is
class User < ActiveRecord::Base
attr_accessible :email, :pic, :name, :username
has_many :topics
end
My migration for CreateUsers
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email
t.string :username
t.string :pic
t.timestamps
end
end
end
Now what I plan to do is run
rails g migration AddDeviseColumnsToUser
And add this to my migration file
class AddDeviseColumnsToUser < ActiveRecord::Migration
def change
change_table :users do |t|
t.string :encrypted_password, :null => false, :default => '', :limit => 128
t.confirmable
t.recoverable
t.rememberable
t.trackable
t.token_authenticatable
t.timestamps
end
end
end
Now I am wondering how should I setup my routes or do I have to ? And what attributes should be made accessible in my User model?
Update: I have already installed Devise and configured it with
rails generate devise:install
Add Devise for existing model First, add a column called "email" if you don't already have one. Second, make sure that every existing row has a unique value for email. Finally, go into the migration file called "add_devise_to_users" and comment out the line that adds an email column. and restart your server.
To run a specific migration up or down, use db:migrate:up or db:migrate:down . The version number in the above commands is the numeric prefix in the migration's filename. For example, to migrate to the migration 20160515085959_add_name_to_users. rb , you would use 20160515085959 as the version number.
You must rollback the migration (for example with bin/rails db:rollback ), edit your migration, and then run bin/rails db:migrate to run the corrected version.
When you create a User model with Devise using the command rails generate devise User . Devise will create default columns in the database for your User model. To add your own columns for your users you can create a new Rails migration and add any columns you want for your users.
Just add devise_for :user
in your routes
Add attr_accessible :password, :password_confirmation
and for more info take a look at a typical devise model
https://github.com/johndel/Rails-Simple-CMS/blob/master/app/models/admin.rb
(Pretty simple)
You can simply run:
rails generate devise User
to add devise to the User model.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With