ActiveRecord :: RecordNotFound - before_action set_article

时间:2015-01-20 09:04:38

标签: ruby-on-rails ruby activerecord

我正在尝试创建一个Rails博客应用程序,并且在实现友好网址时遇到了一些问题。 (不是通过gem,我手动实现它们。)我能够访问文章的显示页面,但是当我尝试喜欢/不同于文章或发表评论时,我收到此错误: ActiveRecord的:: RecordNotFound。这是它标记的代码片段:

Extracted source (around line #19)

  def set_article
    @article = Article.find_by!(slug: params[:id])
  end
end

这是我实现的三个控制器:

class ArticlesController < ApplicationController
  before_action :require_signin, except: [:index, :show]
  before_action :require_admin, except: [:index, :show]
  before_action :set_article, only: [:show, :edit, :update, :destroy]

  # GET /articles
  # GET /articles.json
  def index
    @articles = Article.all
  end

  # GET /articles/1
  # GET /articles/1.json
  def show
    @likers = @article.likers

    if current_user
      @current_like = current_user.likes.find_by(article_id: @article.id)
    end

    @tags = @article.tags
  end

  # GET /articles/new
  def new
    @article = Article.new
  end

  # GET /articles/1/edit
  def edit
  end

  # POST /articles
  # POST /articles.json
  def create
    @article = Article.new(article_params)

    respond_to do |format|
      if @article.save
        format.html { redirect_to @article, notice: 'Article was successfully created.' }
    format.json { render :show, status: :created, location: @article }
  else
    format.html { render :new }
    format.json { render json: @article.errors, status: :unprocessable_entity }
  end
end
  end

  # PATCH/PUT /articles/1
  # PATCH/PUT /articles/1.json
  def update
    respond_to do |format|
      if @article.update(article_params)
        format.html { redirect_to @article, notice: 'Article was successfully updated.' }
        format.json { render :show, status: :ok, location: @article }
      else
        format.html { render :edit }
        format.json { render json: @article.errors, status: :unprocessable_entity }
  end
end
  end

  # DELETE /articles/1
  # DELETE /articles/1.json
  def destroy
    @article.destroy
    respond_to do |format|
      format.html { redirect_to articles_url, notice: 'Article was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_article
      @article = Article.find_by!(slug: params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def article_params
      params.require(:article).permit(:slug, :title, :content, tag_ids: [])
    end
end

class LikesController < ApplicationController

before_action:require_signin   before_action:set_article

def create
  @article.likers << current_user
  redirect_to @article, notice: "Thanks for liking!"
end

def destroy
  like = current_user.likes.find(params[:id])
  like.destroy
  redirect_to @article, notice: "Sorry you unliked it!"
end

private

  def set_article
    @article = Article.find_by!(slug: params[:id])
  end
end

class CommentsController < ApplicationController
  before_action :set_article
  before_action :set_comment, only: [:edit, :update, :destroy]

  # GET /comments
  # GET /comments.json
  def index
    @comments = @article.comments
  end

  # GET /comments/new
  def new
    @comment = @article.comments.new
  end

  # GET /comments/1/edit
  def edit

  end

  # POST /comments
  # POST /comments.json
  def create
    @comment = @article.comments.new(comment_params)
    respond_to do |format|
      if @comment.save
        format.html { redirect_to @article, notice: 'Comment was successfully created.' }
    format.json { render :show, status: :created, location: @article }
      else
        format.html { render :new }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
  end
end
  end

  # PATCH/PUT /comments/1
  # PATCH/PUT /comments/1.json
  def update
    respond_to do |format|
      if @comment.update(comment_params)
        format.html { redirect_to article_path(@article), notice: 'Comment was successfully updated.' }
        format.json { render :show, status: :ok, location: article_comments_path(@article) }
      else
        format.html { render :edit }
        format.json { render json: @article.comments.errors, status: :unprocessable_entity }
  end
end
  end

  # DELETE /comments/1
  # DELETE /comments/1.json
  def destroy
    @article.comments.destroy
    respond_to do |format|
      format.html { redirect_to comments_url, notice: 'Comment was successfully destroyed.' }
      format.json { head :no_content }
end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_article
      @article = Article.find_by!(slug: params[:id])
    end

    def set_comment
      @comment = Comment.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def comment_params
      params.require(:comment).permit(:post)
    end
end

这是文章模型

class Article < ActiveRecord::Base
before_validation :generate_slug
has_many :comments, dependent: :destroy
has_many :likes, dependent: :destroy
has_many :likers, through: :likes, source: :user
has_many :characterizations, dependent: :destroy
has_many :tags, through: :characterizations
belongs_to :user


validates :title, presence: true, uniqueness: true

validates :content, presence: true

validates :slug, uniqueness: true

def to_param
  slug
end

def generate_slug
  self.slug ||= title.parameterize if title
end
end

我不确定为什么会遇到问题。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

您的数据库中似乎没有article slugparams[:id],这就是它抛出错误的原因。您正在使用find_by!,如果找不到记录,则会引发错误。

LikesController

...
private
  def set_article
    @article = Article.find_by!(slug: params[:article_id])
  end
...
  # You need to use params[:article_id] not params[:id], because in your route for like it is set as article_id
  # If same set up for comments then just change params[:id] -> params[:article_id]