简体   繁体   English

印象派宝石和Friendly_id

[英]Impressionist gem and Friendly_id

I have a Rails app with impressionist and friendly id, I want to show page views in my articles. 我有一个带有印象派和友好ID的Rails应用,我想在文章中显示页面浏览量。 The problem is that impressionist is not registering the number of views. 问题是,印象派画家没有注册观看次数。

It's the same problem here on impressionist's bug tracker . 在印象派专家的错误跟踪器上,这是同样的问题。

My articles model: 我的文章模型:

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

Articles controller: 文章控制者:

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

I also restarted the server, but having the same issue. 我也重新启动了服务器,但是有同样的问题。

Ok there is a work around. 好的,可以解决。 Just add impressionist(@model) in the show controller 只需在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

Little tip for unique functionality: 独特功能的小技巧:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM