-2
我想讓用戶可以爲各種咖啡店添加書籤。我已經成功設法讓用戶喜歡咖啡店,所以試圖按照相同的步驟,但我試圖加載Coffeeshops#show
時得到一個NoMethodError
。NoMethodError Rails 5
我的代碼:
user.rb
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :validatable,
has_many :comments
has_many :coffeeshops
has_many :favorite_coffeeshops # just the 'relationships'
has_many :favorites, through: :favorite_coffeeshops, source: :coffeeshop
has_many :bookmarked_coffeeshops # just the 'relationships'
has_many :bookmarks, through: :bookmarked_coffeeshops, source: :coffeeshop
coffeeshop.rb
class Coffeeshop < ApplicationRecord
has_many :comments, as: :commentable
belongs_to :roaster
belongs_to :user
has_many :favorite_coffeeshops# just the 'relationships'
has_many :favorited_by, through: :favorite_coffeeshops, source: :user
has_many :bookmarked_coffeeshops# just the 'relationships'
has_many :bookmarked_by, through: :bookmarked_coffeeshops, source: :user
bookmarked_coffeeshop.rb
class BookmarkedCoffeeshop < ApplicationRecord
belongs_to :coffeeshop
belongs_to :user
end
coffeeshop.show.html.erb
<%= link_to "Bookmark", bookmarked_coffeeshops_path(@coffeeshop, type: "bookmarked"), method: :put %>
遷移
class CreateBookmarkedCoffeeshops < ActiveRecord::Migration[5.0]
def change
create_table :bookmarked_coffeeshops do |t|
t.integer :coffeeshop_id
t.integer :user_id
t.timestamps
end
end
end
錯誤
undefined method `bookmarked_coffeeshop_path' for #<#<Class:0x007fadb9ccad30>:0x007fadc3232e90>
確切的錯誤是什麼?並添加相應的代碼,在該行上引發錯誤。 –
剛剛添加到問題 –
'bookmarked_coffeeshop_path'或'bookmarked_coffeeshops_path'我找不到'showed'視圖中的'bookmarked_coffeeshop_path'。分享你的路線和'耙路線' –