First and last objects in rails collection -
in application have "students" partial, passes collection of @students "student" partial. these displayed on index page images of students. when user clicks on image, i'm poping box student's photo , text. now, have previous/next button allows users cycle through students. implemented cycling w/out plugin...just simple ajax call load next/previous object. problem cannot detect when i'm @ last (or first) record. need way set url parameter on first , last objects. this: www.mysite.com/students/15?last=true
i couldn't find clean way this. i'm not sure if i'm thinking right way. should attaching url parameters? or should somehow modify object add additional attributes after retrieving them model?
thank you.
[edit]
just wanted make little clearer. when pop students lightbox, have 2 links: previous (linking previous record) , next linking next record. i'm using jquery $.load method make ajax request , load in data want, div in lightbox, whenever click previous or next.
$('previous').click( $('mydiv').load('url of previous student'); );
i 'url of previous/next student' walking dom tree on initial pop up. store these can refer them again.
my problem cannot tell when i'm @ first student in list or last. need know can disable prev/next button. i'm thinking can either through rails/ruby or through javascript. initially, thought adding url parameter on link or previous/next student (to indicate whether first or last), detect in rails , simple if/else disable links. got stuck @ trying way i'm referring partials.
ehm, way inspiration,maybe you: student/index classic index file ajax loading list, will_paginate , code wrote above. every student have image link, link has href /student/4 , remote: true.
student show controller
@student = student.find(params[:id]) student show js file classic js render "student"
student _student.html.erb view , 2 view helper: prev_help(params[:id]) , next_help(params[:id])
helpers:
def prev_help(id) @student = student.find(id - 1) link_to("prev". @student ) if @student.nil? end def next_help(id) @student = student.find(id + 1) link_to("prev". @student ) if @student.nil? end i don't have time now, if solution not you, can write me tomorrow , i'll create sample app test solution , colaborate you.
Comments
Post a Comment