Activerecord 信誉系统 - 嵌套路由

Activerecord reputation system - nested routes

我正在尝试观看此视频 railscast #364,但我在使用嵌套路由时遇到了很多问题。当我使用此代码时:

<%= link_to "up", vote_movie_review_path(@movie, @reviews, type: "up"), method: "post" %>

我在 select 赞成票时遇到此错误:

ActiveRecord::RecordNotFound in ReviewsController#vote 

Couldn't find Review with 'id'=# <Review::ActiveRecord_Relation:0x007f0358c1e550>

这是我的路线:

vote_movie_review POST   /movies/:movie_id/reviews/:id/vote(.:format) genre_linkers#vote

我使用以下代码创建了另一个未嵌套的模型:

<%= link_to "up", vote_movie_path(movie, type: "up"), method: "post" %>

那个成功了。所以我认为我的路径或我调用对象的方式一定有问题。我几乎整天都在研究这个,我真的需要帮助。

review_controller.rb

class ReviewsController < ApplicationController
  before_action :set_review, only: [:show, :edit, :update, :destroy]
  before_action :set_movie
  before_action :authenticate_user!
  respond_to :html

  def index
    @reviews = Review.all
    respond_with(@reviews)
  end

  def show

  end

  def vote
    value = params[:type] == "up" ? 1 : -1
    @review = Review.find(params[:id])
    @review.add_evaluation(:vote, value, current_user)
    redirect_to :back, notice: "thanks for the vote"
  end


  def new
    @review = Review.new
    respond_with(@review)
  end

  def edit
  end

  def create
    @review = Review.new(review_params)
    @review.user_id = current_user.id
    @review.movie_id = @movie.id

    if @review.save
      redirect_to @movie
    else
      render 'new'
    end
  end

  def update
    @review.update(review_params)
    respond_with(@review)
  end

  def destroy
    @review.destroy
    respond_with(@review)
  end

  private
  def set_review
    @review = Review.find(params[:id])
  end

  def set_movie
    @movie = Movie.find(params[:movie_id])
  end

  def review_params
    params.require(:review).permit(:genre, :description, :vote)
  end

end

routes.rb

Rails.application.routes.draw do

  devise_for :users
  resources :movies do 
    resources :reviews  do 
      member { post :vote }
    end
  end

   root 'movies#index'


end

和模型 review.rb

class Review < ActiveRecord::Base
   belongs_to :user
   belongs_to :movie

   has_reputation :votes, source: :user, aggregated_by: :sum
end

罪魁祸首是您 link_to 的这一部分:vote_movie_review_path(@movie, @reviews, type: "up")。 @reviews 是 ActiveRecord::Relation 而不是评论记录,因此找不到具有 ID 的记录。