【问题标题】:NoMethod Error with on a `favorites' join model Rails 5.2在“收藏夹”连接模型 Rails 5.2 上出现 NoMethod 错误
【发布时间】:2019-01-13 16:02:08
【问题描述】:

我正在尝试让User 收藏coffee_roast。然而,在尝试加载显示页面时,我得到一个 NoMethod 错误

未定义的方法`favorite_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

为什么我找不到正确的路径?我错过了什么?

【问题讨论】:

  • 我可以知道你的 config/routes.rb
  • 刚刚添加的路线
  • 您好像忘记在路由文件中添加favorite_coffeeroast 操作了:) 之后您可以在终端中使用rake routes | grep favorite 命令找到正确的路径名
  • 感谢@Vasilisa 我添加了resource :favorite_coffeeroast 并且页面现在加载,但是当尝试“收藏”时我得到uninitialized constant FavoriteCoffeeroastController

标签: ruby-on-rails activerecord associations


【解决方案1】:

看来您需要了解更多关于 routing in ROR 的信息。将新操作添加到现有的resources :coffee_roasts:

resources :coffee_roasts do
  put :favorite_coffeeroast, on: :member
  resources :comments
end

此外,当您编写带有和不带有嵌套的路由时,您正在复制路由。由于您想在任何地方嵌套 cmets 路由,您可以在不嵌套的情况下删除相应的路由

【讨论】:

  • 问题是,我可以完美地与另一个模型coffeeshops 合作。我不需要像这样更新路线。我只是想复制我在那里所做的事情,但是这个路径问题一直在阻止我。例如,在您上面的 sn-p 中,您有 on: :member 但我已经删除了成员模型。是的,我需要清理我的路线文件。
  • 对不起,我听不懂你的意思。您不想为控制器操作创建新路由,但希望它工作? on: :member 只是告诉你,路线应该如何,是否有 :id 段
猜你喜欢
  • 1970-01-01
  • 2011-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多