【问题标题】:Rails: syntax error, unexpected tIDENTIFIER, expecting keyword_endRails:语法错误,意外的 tIDENTIFIER,期待关键字_end
【发布时间】:2015-06-12 13:07:29
【问题描述】:

Ruby on Rails 初学者在这里。

在 localhost:3000 中出现此错误

ActiveRecord::PendingMigrationError 迁移待定。要解决此问题,请运行:bin/rake db:migrate RAILS_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 中的书签/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/ 我只做了“需要身份验证来管理你的书签”

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

【问题讨论】:

  • 教程底部的注释指出add belongs_to :user应该指向app/model/bookmark.rb

标签: ruby-on-rails ruby ruby-on-rails-3 terminal syntax-error


【解决方案1】:

错误告诉您问题出在哪里。

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

【讨论】:

  • 看教程,我明白你为什么困惑了。它告诉您将 has_many :bookmarks 添加到您的文件 app/models/user.rb
  • 嗨,我刚刚发布了一个新问题,如果您能帮助我,我将不胜感激。你上次回答我了。
【解决方案2】:

模型之间的关系在模型中,迁移是针对数据库的,它是不同的......你应该保留add_column :bookmarks, :user_id, :integer,但其他两行从你的迁移中删除它们,你应该去你的user.rb模型并添加has_many :bookmarks 并转到您的bookmark.rb 模型并添加belongs_to :user

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多