在ArticlesController中充当可选择的轨道ActiveRecord :: RecordNotFound和NoMethodError

时间:2016-03-11 01:50:49

标签: ruby-on-rails activerecord nomethoderror acts-as-votable

我已经完成了在Rails中制作博客应用程序的Udemy课程。我已经为移动查看文章和注册/登录添加了JSON功能。所有工作。

我的下一个问题是我想添加upvotes和downvotes,因此登录的用户可以对文章进行投票。

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

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

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

如果我点击Upvote:

enter image description here

如果我点击Downvote: enter image description here

文章控制者:

    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

enter image description here

1 个答案:

答案 0 :(得分: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