2017-09-30 29 views
0

我不明白为什么会发生这种情况,似乎即使找到了friendly.id文章的id也被查询。即使在与评论关联时找到正确的article_id(59),我发现的任何文章的ID都为“0”。Ruby on Rails文章加载回滚在show.html.erb

Processing by ArticlesController#show as HTML 
    Parameters: {"id"=>"javascript-8"} 
    User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]] 
    Article Load (0.7ms) SELECT "articles".* FROM "articles" WHERE "articles"."slug" = $1 LIMIT $2 [["slug", "javascript-8"], ["LIMIT", 1]] 
    (1.9ms) BEGIN 
    Article Load (0.3ms) SELECT "articles".* FROM "articles" WHERE "articles"."id" = $1 LIMIT $2 [["id", 0], ["LIMIT", 1]] 
    (1.1ms) ROLLBACK 
    Rendering articles/show.html.erb within layouts/application 
    Rendered comments/_comment_form.html.erb (19.3ms) 
    Comment Load (0.7ms) SELECT "comments".* FROM "comments" WHERE "comments"."article_id" = 59 ORDER BY created_at DESC 
    Rendered comments/_comment.html.erb (25.2ms) 

编辑:文章控制器

class ArticlesController < ApplicationController 
    before_action :authenticate_user! 
    before_action :set_article, only: [:show, :edit, :update, :destroy, :toggle_vote] 
    impressionist :actions=>[:show] 

    def show 
    @article_categories = @article.categories 
    @comments = Comment.where(article_id: @article).order("created_at DESC") 

    if @article.status == "draft" && @article.user != current_user 
     redirect_to root_path 
    end 
    end 

    private 

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

end 
+0

发布您的过滤器或操作之前 - 其中“@ article”本身是否正在加载? –

+0

可否请您将代码呈现给文章链接。谢谢 – Hirurg103

+0

@CodyCaughlan使用之前的过滤器编辑 – doyz

回答

0

friendly_id:FOO,使用:猛击#你必须做 MyClass.friendly.find( '巴')

或...

friendly_id:foo,使用方法:[:猛击,:发现者]#你现在可以做 MyClass.find( '巴')

在你的情况

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

这是因为我用印象派的宝石。

我应该在控制器的顶部除去

impressionist :actions=>[:show] 

,而是在表演中添加此F你使用friendly_id

def show 
    impressionist(@article) 
end 

写在印象派宝石使用指南(https://github.com/charlotte-ruby/impressionist#usage),”请确保以这种方式记录印象派,因为params [:id]将返回一个字符串(url slug),而impressionable_id是数据库中的整数列。“