簡體   English   中英

Rails:語法錯誤,意外的tIDENTIFIER,預期keyword_end

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

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中的書簽/數據庫/遷移中的代碼:

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 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM