In my application I have a “Students” partial, that passes a collection of @students to a “student” partial. These are eventually displayed on my index page as images of students. When a user clicks on an image, I’m poping up a box with the student’s photo and some text. Now, I have a previous/next button that allows users to cycle through the students. I implemented the cycling w/out a plugin…just a simple ajax call to load the next/previous object. My problem is that I cannot detect when I’m at the last (or first) record. I need a way to probably set a url parameter on the first and last objects. So It would look like this: http://www.mysite.com/students/15?last=true
I couldn’t find a clean way to do this. I’m also not sure if I’m thinking about this the right way. Should I be attaching url parameters? Or should I somehow modify the object to add additional attributes after retrieving them from the model?
Thank you.
[EDIT]
Just wanted to make this a little clearer.
When I pop up the students lightbox, I have 2 links: previous (linking to the previous record) and next linking to the next record. I’m using the jquery $.load method to make an ajax request and load in the data I want, into a div in my lightbox, whenever I click previous or next.
$(‘previous’).click(
$(‘mydiv’).load(‘url of previous student’);
);
I get the ‘url of previous/next student’ by walking the dom tree on initial pop up. I store these so I can refer to them again.
My problem is that I cannot tell when I’m at the first student in the list or the last. I need to know this so I can disable the prev/next button. I’m thinking I can either do this through rails/ruby or through javascript. Initially, I thought that by adding a url parameter on the link or the previous/next student (to indicate whether they were first or last), then I could detect this in rails and do a simple if/else to disable the links. But I got stuck at trying to do this with the way I’m referring to the partials.
ehm, I do it with my way for inspiration,maybe help you:
Student/index
Classic index file with ajax loading list, will_paginate and code which I wrote above. Every student have image with link, link has href something like /student/4 and remote: true.
student show controller
student show js file
classic js render “student”
student _student.html.erb
your view and two view helper: prev_help(params[:id]) and next_help(params[:id])
helpers:
I don’t have time now, but If this solution will not help you, you can write me tomorrow and I’ll create sample app to test my solution and colaborate it with you.