Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Step on how to add devise migration to existing User model in ruby on rails?

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
like image 434
psharma Avatar asked Feb 27 '13 10:02

psharma


People also ask

How do I add devise to an existing model?

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.

How do I run a specific migration in Rails?

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.

Which command is used to to rollback migration in Rails?

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.

How do I add a column in devise gem?

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.


2 Answers

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)

like image 65
Aggelos Avgerinos Avatar answered Oct 13 '22 00:10

Aggelos Avgerinos


You can simply run:

rails generate devise User

to add devise to the User model.

like image 41
gnerkus Avatar answered Oct 13 '22 02:10

gnerkus