我不明白为什么会发生这种情况,似乎尽管找到了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)编辑:文章控制器
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发布于 2017-10-01 02:04:19
friendly_id :foo,使用::slugged #你必须使用MyClass.friendly.find('bar')
或者..。
friendly_id :foo,使用::slugged,:finders #你现在可以使用MyClass.find('bar')
在你的情况下
def set_article
@article = Article.friendly.find(params[:id])
end发布于 2017-10-01 13:25:04
那是因为我用的是印象派宝石。
我应该删除
impressionist :actions=>[:show] 在控制器的顶部,而不是在show中添加这个
def show
impressionist(@article)
end正如印象派gem使用指南(https://github.com/charlotte-ruby/impressionist#usage)中所写的,“如果你使用的是friendly_id,请确保以这种方式登录印象派,因为参数:id将返回一个字符串(Url slug),而impressionable_id是数据库中的一个整型列。”
https://stackoverflow.com/questions/46505482
复制相似问题