2017-09-30 13 views
0

왜 이런 일이 발생하는지 이해할 수 없지만 friendly.id를 찾았음에도 기사의 ID가 쿼리되고있는 것 같습니다. 내가 발견 한 ID는 "0"입니다. 정확한 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) 

편집 : 기사 컨트롤러

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)을 반환하므로 인상파를이 방법으로 기록하십시오.