0

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

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

我已经安装了acts_as_votable gem并遵循了一个tute(http://www.mattmorgante.com/technology/votable)如何实现它,但是当用户点击upvote/downvote时出现以下错误:ActiveRecord::RecordNotFound 或 NoMethodError在文章控制器中

我猜第一个错误是因为文章控制器已经知道当我点击upvote时我在谈论哪篇文章?所以我评论了 downvote_by 并且它不知道方法 downvote_by

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

如果我点击Upvote:

在此处输入图像描述

如果我点击 Downvote: 在此处输入图像描述

文章控制器:

    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 文件:

<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

在此处输入图像描述

4

1 回答 1

1

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

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

观察等号之间的空格

编辑你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
于 2016-03-11T02:01:02.313 回答