Ruby on rails Rails:语法错误,意外的tIDENTIFIER,应为关键字\u end

Ruby on rails Rails:语法错误,意外的tIDENTIFIER,应为关键字\u end,ruby-on-rails,ruby,ruby-on-rails-3,terminal,syntax-error,Ruby On Rails,Ruby,Ruby On Rails 3,Terminal,Syntax Error,RubyonRails初学者 在localhost:3000中出现此错误 ActiveRecord::PendingMigrationError 迁移挂起。要解决此问题,请运行:bin/rake db:migrate RAILS\u ENV=development 我在终端中运行了rake db:migrate,得到了以下结果: $ rake db:migrate rake aborted! SyntaxError: /Users/EuphoriaComplex/src/bookmarks/db

RubyonRails初学者

在localhost:3000中出现此错误

ActiveRecord::PendingMigrationError 迁移挂起。要解决此问题,请运行:bin/rake db:migrate RAILS\u ENV=development

我在终端中运行了rake db:migrate,得到了以下结果:

$ rake db:migrate
rake aborted!
SyntaxError: /Users/EuphoriaComplex/src/bookmarks/db/migrate/20150407050503_add_user_to_bookmark.rb:5: syntax error, unexpected tIDENTIFIER, expecting keyword_end

    add has_many :bookmarks to app/models/user.rb
                              ^
/Users/EuphoriaComplex/src/bookmarks/db/migrate/20150407050503_add_user_to_bookmark.rb:7: syntax error, unexpected tIDENTIFIER, expecting keyword_end

    add belongs_to :user to app/model/user.rb
                           ^
这是我在Sublime中bookmarks/db/migrate中的代码:

class AddUserToBookmark < ActiveRecord::Migration
  def change
    add_column :bookmarks, :user_id, :integer

    add has_many :bookmarks to app/models/user.rb

    add belongs_to :user to app/model/user.rb
  end
end
class AddUserToBookmark
我遵循本教程: 我只做了“需要身份验证才能管理书签”


“用户有许多书签”是有问题的部分。

错误会准确地告诉您问题所在

add has_many :bookmarks to app/models/user.rb
add belongs_to :user to app/model/user.rb
这不应该出现在迁移中,因为它们不会更改架构。您需要将这些添加到书签和用户模型中,以便

class Bookmark < ActiveRecord::Base
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :bookmarks
end
类书签
模型之间的关系在模型中,迁移是针对数据库的,这是不同的。。。你应该保留
add\u列:bookmarks,:user\u id,:integer
,但是其他两行从迁移中删除它们,你应该转到你的
user.rb
模型和add
有很多:bookmarks
并转到你的
bookmark.rb
模型和add
属于:user


也许您也可以阅读本指南,它可能会有所帮助:

教程底部的一条评论指出,
添加属于:user
应该指向
app/model/bookmark.rb
查看教程,我可以理解您为什么感到困惑。它告诉你要在你的文件app/models/user.rbhi中添加很多书签。我刚刚发布了一个新问题,如果你能帮助我,我将不胜感激。上次你回答了我。