2017-09-06 11 views
0

私は、このstackoverflowのポスト以下、How can i store and show only the last 5 NEWS seen by the current user?現在のユーザーが最後に表示した5件の記事だけを表示しますか?

は、しかし、私は私が記事を生成するfriendly_id宝石を使用していますので、私は、(「ID」=のpython-0との記事を見つけることができませんでした)このエラーを取得しています考えていますurl。このための回避策はありますか?ドキュメントを1として

アプリケーションコントローラ

before_action :recently_viewed_articles 

    def recently_viewed_articles 
    session[:article_id] ||= [] 
    session[:article_id] << params[:id] unless params[:id].nil? 
    session[:article_id].delete_at(0) if session[:article_id].size >= 5 
    end 

ページのコントローラー

def home 
    @recent_articles = session[:article_id] 
    @feed = current_user.feed 
end 

_feed.html.erb

<div class = "container"> 
    <div class = "row"> 
    <div class = "col-md-9"> 
     <%= render @feed %> 
    </div> 
    <div class = "col-md-3"> 
     <div class="list-group"> 
      <button type="button" class="list-group-item list-group-item-action active"> 
      Recently visited 
      </button> 
     <% @recent_articles.each do |recent| %> 
      <button type="button" class="list-group-item list-group-item-action"> 
      <%= link_to "#{Article.find(recent).title}", article_path(Article.find(recent)) %> 
      </button> 
     <% end %> 
     </div> 
    </div> 
    </div> 
</div> 

答えて

0

、私はあなたがfriendlyスコープを使用する必要があると考えています。

Article.friendly.find(recent) 
+0

おお呼び出すことができます。ありがとう! – doyz

0

https://github.com/norman/friendly_id

あなたが行う必要があり、すべてを設定することがありますか?あなたがいない場合は、使用することができます。

Article.friendly.find('python-0') 

をそれ以外の場合は、あなたの記事のモデルに:

friendly_id :title, use: [:slugged, :finders] 

は、その後、あなたがこの仕事を

Article.find('python-0') 
関連する問題