Render Prev & Next Record From Show Page in Rails

Scenario:
In Rails we can click individual record and open it from index page but to open the next record we need to go back to index & click another link.  But we can have a Prev / Next Navigation options in our show page to render our previous/next records without clicking individual links.

We will be doing this : 1 . Add Prev & Next links in our View Page

2. Prepare methods in Model file to update current record

1 . Add Prev & Next links in our View Page
Add following links to your show page (app/views/feedbacks/show.html.erb)

#Hide 'Previous' link once we reach to first record
              
  
 
 
   
#Hide 'Next' link once we reach to last record
      
  
           
                  

2. Prepare methods in Model file to update current record

def self.get_previous_feedback(current_feedback)
       
  Feedback.where("feedbacks.id < ? ", current_feedback.id).order('created_at asc').last
            end


          def self.get_next_feedback(current_feedback,current_user)
                   
 Feedback.where("feedbacks.id > ? ", current_feedback.id).order('created_at asc').first
          end

Now just open your show page in browser and use the prev/next button to render respective records in show.html.erb.
We can make this more efficient by adding ajax flavour to our show page and stop our complete rendering of our page. We’ll do that in the next Tip!!!

150 150 Burnignorance | Where Minds Meet And Sparks Fly!