我不明白爲什麼會發生這種情況,似乎即使找到了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
發佈您的過濾器或操作之前 - 其中「@ article」本身是否正在加載? –
可否請您將代碼呈現給文章鏈接。謝謝 – Hirurg103
@CodyCaughlan使用之前的過濾器編輯 – doyz