首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >show.html.erb上的Ruby on Rails项目加载回滚

show.html.erb上的Ruby on Rails项目加载回滚
EN

Stack Overflow用户
提问于 2017-10-01 01:51:53
回答 2查看 54关注 0票数 0

我不明白为什么会发生这种情况,似乎尽管找到了friendly.id,但仍在查询文章的id。对于我点击的任何文章,找到的ID都是"0“,即使在与评论关联时找到了正确的article_id (59)。

代码语言:javascript
复制
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)

编辑:文章控制器

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2017-10-01 02:04:19

friendly_id :foo,使用::slugged #你必须使用MyClass.friendly.find('bar')

或者..。

friendly_id :foo,使用::slugged,:finders #你现在可以使用MyClass.find('bar')

在你的情况下

代码语言:javascript
复制
def set_article
  @article = Article.friendly.find(params[:id])
end
票数 0
EN

Stack Overflow用户

发布于 2017-10-01 13:25:04

那是因为我用的是印象派宝石。

我应该删除

代码语言:javascript
复制
impressionist :actions=>[:show] 

在控制器的顶部,而不是在show中添加这个

代码语言:javascript
复制
def show
  impressionist(@article)
end

正如印象派gem使用指南(https://github.com/charlotte-ruby/impressionist#usage)中所写的,“如果你使用的是friendly_id,请确保以这种方式登录印象派,因为参数:id将返回一个字符串(Url slug),而impressionable_id是数据库中的一个整型列。”

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46505482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档