2017-09-30 17 views
0

なぜこれが起こっているのか分かりませんが、friendly.idが見つかったにもかかわらず記事のIDが照会されているようです。見つかったIDは、コメントと関連付けられたときに正しいarticle_id(59)が見つかったとしても、クリックした記事の「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) 

EDITED:記事コントローラ

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はbeforeフィルターで編集しました – 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] 

を削除し、代わりにあなたが

印象派の宝石の利用ガイド( https://github.com/charlotte-ruby/impressionist#usage)で書かれたよう
def show 
    impressionist(@article) 
end 

を、「friendly_id使用しているfのショーでこれを追加する必要がありますimpressable_idがデータベースの整数列である間にparams [:id]が文字列(url slug)を返すので、印象派をこのように記録してください。

関連する問題