简体   繁体   English

Rails:语法错误,意外的tIDENTIFIER,预期keyword_end

[英]Rails: syntax error, unexpected tIDENTIFIER, expecting keyword_end

Ruby on Rails beginner here. Ruby on Rails初学者在这里。

Had this error in localhost:3000 在localhost:3000中发生此错误

ActiveRecord::PendingMigrationError Migrations are pending. ActiveRecord :: PendingMigrationError迁移正在挂起。 To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development 要解决此问题,请运行:bin / rake db:migrate RAILS_ENV = development

I ran rake db:migrate in terminal and got this: 我在终端中运行了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
                           ^

And this is my code in bookmarks/db/migrate in Sublime: 这是我在Sublime中的书签/数据库/迁移中的代码:

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

I was following this tutorial: http://12devs.co.uk/articles/writing-a-web-application-with-ruby-on-rails/ and I only made it to "Require authentication to manage your bookmarks" 我正在遵循本教程: http : //12devs.co.uk/articles/writing-a-web-application-with-ruby-on-rails/,而我只是将其设置为“需要身份验证才能管理您的书签”。

"Users have many Bookmarks" is the section in question. 问题的是“用户有很多书签”

The error tells you exactly where the problem is. 该错误告诉您问题的确切出处。

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

this should not be in a migration since they do not change the schema. 这不应进行迁移,因为它们不会更改架构。 You need to add these to the bookmark and user model, so 您需要将它们添加到书签和用户模型中,因此

class Bookmark < ActiveRecord::Base
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :bookmarks
end

The relationships between models go in the models, the migrations are for the database, it is different... you should keep the add_column :bookmarks, :user_id, :integer but the other two lines erase them from your migration, you shouldgo to your user.rb model and add has_many :bookmarks and go to your bookmark.rb model and add belongs_to :user 模型之间的关系进入模型,迁移是针对数据库的,这是不同的...您应保留add_column :bookmarks, :user_id, :integer但其他两行将从迁移中删除它们,您应该转到user.rb模型并添加has_many :bookmarks并转到您的bookmark.rb模型并添加belongs_to :user

Maybe you can also read this guide, it might help: http://guides.rubyonrails.org/index.html 也许您也可以阅读本指南,它可能会有所帮助: http : //guides.rubyonrails.org/index.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HAML:语法错误,意外的 tIDENTIFIER,期待关键字结束 - HAML: syntax error, unexpected tIDENTIFIER, expecting keyword_end 语法错误,意外的tIDENTIFIER,期待keyword_end对话控制器 - syntax error, unexpected tIDENTIFIER, expecting keyword_end conversations controller 语法错误,意外的tIDENTIFIER,需要keyword_end - syntax error, unexpected tIDENTIFIER, expecting keyword_end 语法错误,意外的tIDENTIFIER,期待keyword_end - syntax error, unexpected tIDENTIFIER, expecting keyword_end 语法错误,意外的tIDENTIFIER,期待keyword_end - syntax error, unexpected tIDENTIFIER, expecting keyword_end Ruby语法错误:意外的tIDENTIFIER,期望keyword_end - Ruby syntax error: unexpected tIDENTIFIER, expecting keyword_end 语法错误,意外的tIDENTIFIER,期望使用keyword_end simple_form gem - syntax error, unexpected tIDENTIFIER, expecting keyword_end simple_form gem RoR指南第6章不断获取语法错误,出现意外的tIDENTIFIER,期望出现keyword_end(SyntaxError - RoR Tutorial Chapter 6 keep getting syntax error, unexpected tIDENTIFIER, expecting keyword_end (SyntaxError 语法错误,非预期的&#39;(&#39;,需要keyword_end - syntax error, unexpected '(', expecting keyword_end posts_controller.rb:8:语法错误,意外的tIDENTIFIER,期望keyword_end has_many:注释依赖于::destroy - posts_controller.rb:8: syntax error, unexpected tIDENTIFIER, expecting keyword_end has_many :comments dependant: :destroy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM