Я пытаюсь разрешить User
добавить в избранное coffee_roast
. Однако при попытке загрузить страницу показа я получаю сообщение об ошибке NoMethod
неопределенный метод `favour_coffee_roast_path '
, который приходит из этой строки кода:
<%= link_to "Add to favorites", favorite_coffee_roast_path(@coffee_roast, type: "favorite"), method: :put %><br />
Я пробовал несколько вариантов, таких как:
favorite_roast_coffee_roast_path
favorite_coffeeroast_coffee_roast_path
favorite_coffee_roast_coffee_roast_path
Нет, очевидно, работает.
Мои модели
class User < ApplicationRecord
has_merit
has_one :drink
has_many :coffeeshops
has_many :coffee_roasts
has_many :favorite_coffeeroasts
has_many :favorite_roasts, through: :favorite_coffeeroasts, source: :coffee_roast
class CoffeeRoast < ApplicationRecord
has_many :favorite_coffeeroasts
has_many :favorited_by, through: :favorite_coffeeroasts, source: :user
class FavouriteCoffeeroast < ApplicationRecord
belongs_to :coffee_roast
belongs_to :user
Мой контроллер
class CoffeeRoastsController < ApplicationController
...
def favorite_coffeeroast
@coffee_roast = CoffeeRoast.find(params[:id])
type = params[:type]
if type == "favorite"
current_user.favorite_roasts << @coffee_roast
redirect_to @coffee_roast, notice: "You favorited #{@coffee_roast.name}"
elsif type == "unfavorite"
current_user.favorite_roasts.delete(@coffee_roast)
redirect_to @coffee_roast, notice: "Unfavorited #{@coffee_roast.name}"
else
# Type missing, nothing happens
redirect_to @coffee_roast, notice: "Nothing happened."
end
end
routes.rb
Rails.application.routes.draw do
#core root
get 'home/index' => 'home#index'
root 'home#index'
#roasts redirect
get '/roasts', to: redirect('/coffee_roasts', status: 302)
namespace :api do
namespace :v1 do
resources :roasts
end
end
resources :blends
resources :roasters
resources :countries
resources :regions
resources :comments
resources :coffee_flavours
resources :flavours
resources :drinks
devise_for :users
devise_for :admins
resources :varietals
resources :tags
resources :coffee_beans
resources :coffee_roasts
resources :processings
get 'contact-me', to: 'messages#new', as: 'new_message'
post 'contact-me', to: 'messages#create', as: 'create_message'
#static pages
get 'about', to: 'pages#about'
get 'cookiepolicy', to: 'pages#cookiepolicy'
get 'map', to: 'pages#map'
get 'longblack', to: 'longblack#index'
get 'prices', to: 'prices#new'
post 'prices', to: 'prices#create'
#db resources
resources :roasters do
resources :comments
end
resources :articles do
resources :comments
end
resources :coffeeshops do
resources :comments
end
resources :roasts do
resources :comments
end
resources :coffee_roasts do
resources :comments
end
resources :coffeeshops do
put :favorite, on: :member
end
resources :coffeeshops do
put :bookmarked, on: :member
end
Почему я не могу найти правильный путь? Чего мне не хватает?