我无法理解为什么会发生这种情况,即使找到了friendly.id,也似乎正在查询文章的ID。对于我点击的任何文章,找到的ID都是“0”,即使在与评论关联时找到了正确的article_id(59)。
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 :(得分:0)
friendly_id:foo,使用:: slugged#你必须这样做 MyClass.friendly.find( '巴')
friendly_id:foo,使用:[:slugged,:finders]#你现在可以做了 MyClass.find( '巴')
在你的情况下
def set_article
@article = Article.friendly.find(params[:id])
end
答案 1 :(得分:0)
因为我正在使用印象派宝石。
我应该删除
impressionist :actions=>[:show]
在控制器顶部,而不是在show
中添加它def show
impressionist(@article)
end
正如在印象派宝石使用指南(https://github.com/charlotte-ruby/impressionist#usage)中所写,&#34;如果你正在使用friendly_id,请务必以这种方式记录印象派,因为params [:id]将返回一个字符串(url) slug)而impressionable_id是数据库中的Integer列。 &#34;