在 ArticlesController 中充当 votable rails ActiveRecord::RecordNotFound 和 NoMethodError

acts as votable rails ActiveRecord::RecordNotFound and NoMethodError in ArticlesController

我在 Rails 完成了关于制作博客应用程序的 Udemy 课程。我已经添加了 JSON 用于移动查看文章的功能并登录 up/logging。一切正常。

我的下一个问题是我想添加赞成票和反对票,以便登录用户可以对文章进行投票。

我已经安装了 acts_as_votable gem 并遵循了教程 (http://www.mattmorgante.com/technology/votable) 如何实现它,但是当用户单击 upvote/downvote 时出现以下错误: ActiveRecord::RecordNotFound 或 ArticlesController 中的 NoMethodError

我猜第一个错误是因为文章管理员在我点赞的时候已经知道我说的是哪篇文章了?所以我评论说 downvote_by 并且它不知道方法 downvote_by

我错过了什么?感谢帮助。谢谢。

如果我点击赞:

如果我点击反对票:

文章管理员:

    class ArticlesController < ApplicationController
  before_action :authenticate_user!, :except => [:index, :show]
  before_filter :set_article, only: [:show, :edit, :update, :destroy]

  def index
    @articles = Article.all
  end

  def new
    @article = Article.new
  end

  def create
    @article = current_user.articles.build(article_params)
    if @article.save
      flash[:success] = "Article has been created"
      redirect_to articles_path
    else
      flash.now[:danger] = "Article has not been created"
      render :new
    end
  end

  def edit
    if @article.user != current_user
      flash[:danger] = "You can only edit your own article"
      redirect_to root_path
    end
  end 

  def update
    if @article.user != current_user
      flash[:danger] = "You can only edit your own article"
      redirect_to root_path
    else
      if @article.update(article_params)
          flash[:success] = "Article has been updated"
          redirect_to @article
      else
        flash.now[:danger] = "Article has not been updated"
        render :edit
      end
    end
  end

  def show
    @comment = @article.comments.build
  end 

  def destroy
    if @article.destroy
      flash[:success] = "Article has been deleted"
      redirect_to articles_path
    end
  end
  def upvote
    @article=Article.find(params[:id])
    @article.upvote_by current_user
    flash[:success] = "Successfully liked"
    respond_to do |format|
      format.html {redirect_to articles_path }
      format.json { render json: { count: @article.liked_count } }
    end
  end
  def downvote
    #@article = Article.find(params[:id])
    @article.downvote_by current_user
    flash[:success] = "Successfully disliked"
    respond_to do |format|
      format.html {redirect_to articles_path }
      format.json { render json: { count: @article.disliked_count } }
    end
  end

  private 
    def article_params
      params.require(:article).permit(:title, :body)
    end

    def set_article
    @article=Article.find(params[:id])
    end
end

show.html.erb 涉及 likes/dislikes 的文件:

<div class="article-body">
<%= link_to article_like_path(@article), method: :put do %>
  Upvote
    <%= @article.get_upvotes.size %>
<% end %>
<%= link_to article_dislike_path(@article), method: :put do %>
  Downvote
    <%= @article.get_downvotes.size %>
<% end %>

文章模型:

    class Article < ActiveRecord::Base
  validates :title, presence: true
  validates :body, presence: true

  belongs_to :user
  acts_as_votable
  has_many :comments, dependent: :destroy

  default_scope { order(created_at: :desc)}
end 

路线文件:

Rails.application.routes.draw do

  devise_for :users, :controllers => {registrations: "registrations", sessions: "sessions", :omniauth_callbacks => "callbacks"}
  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  #namespace :api, defaults: {format: :json} do
  #  scope :v1 do
  #    mount_devise_token_auth_for 'User', at: 'auth', skip: [:omniauth_callbacks]
      # :controllers => { :sessions => "api/v1/sessions" }, 
  #  end
  #end

  # You can have the root of your site routed with "root"
  root to: 'articles#index'
  resources :articles do
    put "like", to: "articles#upvote"
    put "dislike", to: "articles#downvote"
    resources :comments
  end
  end

从控制器中的 upvotedownvote 操作中删除此代码行:

@article = Article.find(params[:id])

watch the spaces between the equal sign

将您的 before_filter 修改为:

before_action :set_article, only: [:show, :edit, :update, :destroy, :upvote, :downvote]

你的路线应该是:

resources :articles do
  member do
    put "like", to: "articles#upvote"
    put "dislike", to: "articles#downvote"
  end
    resources :comments
end