Rails:语法错误,意外的 tIDENTIFIER,需要 keyword_end

Rails: syntax error, unexpected tIDENTIFIER, expecting keyword_end

Ruby Rails 初学者。

在 localhost:3000

中有这个错误

ActiveRecord::PendingMigrationError 迁移正在等待中。要解决此问题,运行:bin/rake db:migrate RAILS_ENV=development

我 运行 在终端中搜索 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

我正在学习本教程:http://12devs.co.uk/articles/writing-a-web-application-with-ruby-on-rails/ 我只做到了 "Require authentication to manage your bookmarks"

"Users have many Bookmarks" 是有问题的部分。

错误告诉您问题的确切位置。

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_column :bookmarks, :user_id, :integer 但其他两行从你的迁移中删除它们,你应该去您的 user.rb 模型并添加 has_many :bookmarks 并转到您的 bookmark.rb 模型并添加 belongs_to :user

也许您也可以阅读本指南,它可能会有所帮助:http://guides.rubyonrails.org/index.html