Step on how to add devise migration to existing Us

2019-01-23 08:22发布

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

2条回答
一夜七次
2楼-- · 2019-01-23 09:04

You can simply run:

rails generate devise User

to add devise to the User model.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-01-23 09:15

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)

查看更多
登录 后发表回答