简体   繁体   English

如何在ruby on rails上添加设计迁移到现有用户模型?

[英]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. 话虽这么说,我是否需要设置任何其他路由或使用我的用户方法访问atttributes。

So far user model is 到目前为止用户模型是

class User < ActiveRecord::Base
  attr_accessible :email, :pic, :name, :username
  has_many :topics
end

My migration for CreateUsers 我对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 更新:我已经安装了Devise并配置了它

rails generate devise:install

Just add devise_for :user in your routes 只需在路由中添加devise_for :user

Add attr_accessible :password, :password_confirmation 添加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 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. 添加设计到用户模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM