将devise gem添加到Rails 4/mysql项目(表用户已存在) [英] Adding devise gem to a Rails 4 / mysql project (table users already exists)

查看:73
本文介绍了将devise gem添加到Rails 4/mysql项目(表用户已存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Ruby,Rails,Rake,MySQL(在Ubuntu/bash/命令行上)和 devise (管理员1.2.3,devise 3.0.4)的新手.我必须继续到目前为止所做的一切:Rails 4项目正在运行并链接到MySQL数据库(完整,已填充用户和数据).我可以更改控制器和视图.

我必须为项目添加设计.我正在阅读: https://github.com/plataformatec/devise .我的班级名称是 User .所以我做了 rails生成设计用户.

当我执行 rake db:migrate 时,出现以下错误: Mysql2 :: Error:表'users'已经存在:... 当然,因为我已经在我的项目中有一个 users 表.

无论我读过什么( Devise with Rails 4 ),它总是与 create有关用户模型,永远不要如何在已有的用户模型上插入设计.

我应该阅读什么文件?我必须编辑哪个文件以告诉装置这是我的用户模型和表:"?

解决方案

如果Ruby/Rails对您来说是全新的,那您就迷路了.

  • 在"/yourproject/db/migrate/"中查找迁移文件(迁移名称 .rb).此文件由 rails generate devise User ( User 可以由任何模型生成,例如 UserAdmin 客户 ...).该文件告诉您Devise将对数据库进行的修改(需要什么).

  • 仔细查看此文件,并确保您的数据库符合要求.如果您运行 rake db:migrate ,rake将尝试修改您的数据库.也许这是您的错误(尝试创建表User,因为您的表User已经创建).

  • 修改您的迁移文件.您也可以手动更改数据库并删除迁移文件(如果您不希望跟踪迁移的话);当然,在最后一种情况下,您不必运行 rake 命令.

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:" ?

解决方案

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.

这篇关于将devise gem添加到Rails 4/mysql项目(表用户已存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆