Rails: “Next post” and “Previous post” links in my show view, how to?

前端 未结 7 1158
闹比i
闹比i 2020-12-01 00:51

I\'m new in Rails... smile

In my blog aplication I want to have a \"Previous post\" link and a \"Next post\" link in the bottom of my show view.

How do I do

7条回答
  •  半阙折子戏
    2020-12-01 01:35

    I've created gem proximal_records especially for this kind of task and it works on any dynamically created scope in your model.

    https://github.com/dmitry/proximal_records

    Basic example:

    class Article < ActiveRecord::Base
      include ProximalRecords
    end
    
    
    scope = Article.title_like('proximal').order('created_at DESC, title ASC')
    current_record = scope.to_a[5]
    p, n = current_record.proximal_records(scope) # will find record 5 and 7
    

提交回复
热议问题