2017-09-30 31 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是數據庫中的整數列。「