2017-05-23 12 views
-2

Ich versuche Benutzern zu erlauben, verschiedene Cafés zu markieren. Ich habe es erfolgreich geschafft, Benutzern zu Lieblingscafés zu erlauben, also habe versucht, die gleichen Schritte zu folgen, aber ich bekomme NoMethodError, wenn ich versuche, Coffeeshops#show zu laden.NoMethodError Rails 5

Mein Code:

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

Migration

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 

Fehler

undefined method `bookmarked_coffeeshop_path' for #<#<Class:0x007fadb9ccad30>:0x007fadc3232e90> 
+0

Was ist der genaue Fehler? und fügen Sie relevanten Code hinzu, auf dessen Zeile der Fehler geworfen wird. –

+0

gerade hinzugefügt zu der Frage –

+1

'bookmarked_coffeeshop_path' oder' bookmarked_coffeeshops_path' Ich kann 'bookmarked_coffeeshop_path' in Ihrer' show' Ansicht nicht finden. Teilen Sie Ihre Routen und 'Rake Routen' –

Antwort

0

Schoolboy Fehler - ich hadn ‚T meine Routen aktualisiert:

Hinzufügen:

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

das Problem behoben.

Verwandte Themen