ActiveRecord::RecordNotFound - до_действия set_article

Я пытаюсь создать приложение для блога Rails, и у меня возникли проблемы с реализацией дружественных URL. (Не через гем, я вручную их реализую.) Я могу получить доступ к странице показа статьи, однако, когда я пытаюсь понравиться / не понравиться статье или оставить комментарий к статье, я получаю эту ошибку: 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 ответ

Решение

Кажется, у вас нет article с slug значение params[: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]
Другие вопросы по тегам