2017-05-23 5 views
-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> 
+0

正確なエラーは何ですか?エラーが発生した行に関連するコードを追加します。 –

+0

は、 –

+1

の 'bookmarked_coffeeshop_path'または' bookmarked_coffeeshops_path'に追加されました。あなたの 'show'ビューで' bookmarked_coffeeshop_path'を見つけることができません。あなたのルートと 'レーキルート'を共有します –

答えて

0

小学生エラー - I HADN 「tは私のルートを更新:

の追加:

resources :coffeeshops do 
    put :bookmarked, on: :member 
end 

は、問題を解決しました。

関連する問題