印象派宝石和Friendly_id

时间:2012-05-12 21:57:11

标签: ruby-on-rails

我有一个带有印象派和友好ID的Rails应用程序,我想在我的文章中显示页面浏览量。问题在于印象派没有注册观看次数。

这是同样的问题here on impressionist's bug tracker

我的文章模型:

class Article < ActiveRecord::Base
  is_impressionable
  extend FriendlyId
  friendly_id :title, use: [:slugged, :history]
  belongs_to :user
  belongs_to :category

文章控制者:

def show
  @article = Article.find(params[:id])

  if request.path != article_path(@article)
    redirect_to @article, status: :moved_permanently
  end
  respond_to do |format|
     format.html 
     format.json { render json: @article }
  end    
end

我也重新启动了服务器,但遇到了同样的问题。

2 个答案:

答案 0 :(得分:9)

好的,有一个解决方法。只需在show controller

中添加印象派(@model)
def show
  @article = Article.find(params[:id])
  impressionist(@article)

   if request.path != article_path(@article)
      redirect_to @article, status: :moved_permanently
   end
   respond_to do |format|
    format.html 
    format.json { render json: @article }
  end    
end

答案 1 :(得分:1)

独特功能的小提示:

def show
  @article = Article.find(params[:id])
  impressionist(@article, nil, { unique: [:session_hash] })
end