Adding devise gem to a Rails 4 / mysql project (table users already exists)

*爱你&永不变心* 提交于 2019-12-25 04:19:12

问题


I'm a total newbie with Ruby, Rails, Rake, MySQL (on Ubuntu/bash/command line) and devise (warden 1.2.3, devise 3.0.4). I must continue with what have been done so far : the Rails 4 project is working and linked to a MySQL database (full, populated with users and data). I'm able to change controllers and views.

I must add devise to the project. I'm reading this : https://github.com/plataformatec/devise . My class name is User. So I did rails generate devise User.

When I do rake db:migrate I have this error : Mysql2::Error: Table 'users' already exists: ... of course, since I already have a users table in my project.

Whatever I read ( Devise with Rails 4 ), it's always about create the User model, never how to plug devise on a pre-existing User model.

What document should I read ? Which file I must edit to tell devise "this is my user model and table:" ?


回答1:


If Ruby/Rails is brand new for you, and you're lost.. (like I was...).

  • Look for the migration file (migration-name.rb) in "/yourproject/db/migrate/". This file was generated by rails generate devise User (User can by any Model, like UserAdmin or Client ...). This file tells you the modifications Devise will do to the database (what it needs).

  • Look at this file carefully and make sure that your database match the requirements. If you run rake db:migrate rake will attempt to modify your database. Maybe this is your error (attempt to create a table User, since your table User is already created).

  • Modify your migration file. You may also change your database manually and delete the migration file (if you don't care to keep track of your migrations); in this last case, of course, you don't have to run the rake command.




回答2:


Follow the Devise getting-started guide, and intelligently apply the manual steps to your current project where necessary.

https://github.com/plataformatec/devise#getting-started



来源:https://stackoverflow.com/questions/20666790/adding-devise-gem-to-a-rails-4-mysql-project-table-users-already-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!