0

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: 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.

4

2 回答 2

1

嗯,我这样做是为了获得灵感,也许对你有帮助:Student/index Classic index file with ajax loading list, will_paginate 和我上面写的代码。每个学生都有带链接的图片,链接有类似 /student/4 的 href 和 remote: true。

学生表演控制器

@student = Student.find(params[:id])

学生展示js文件经典js渲染“学生”

student _student.html.erb 你的视图和两个视图助手:prev_help(params[:id]) 和 next_help(params[:id])

帮手:

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

我现在没有时间,但是如果此解决方案对您没有帮助,您可以明天给我写信,我将创建示例应用程序来测试我的解决方案并与您合作。

于 2012-06-05T18:22:54.627 回答
1

您需要来自@students 的最后一页还是最后一个学生?我使用 gem will_paginate 来列出页面,使用简单的 JS “ajax”来刷新下一页。您可以使用此 gem 并将每页的对象设置为 1,例如:

@students = Student.order(sort_column + " " + sort_direction).paginate(:per_page => 1, :page => params[:page])

示例“ajax”学生/index.js:

$('tbody').append('<%= j render(@students) %>');
<% if @students.next_page %>
  $('.pagination').replaceWith('<%= j will_paginate(@students) %>');
<% else %>
  $('.pagination').remove();
<% end %>

激活它的咖啡脚本:

if $('.pagination').length
        $(window).scroll ->
          url = $('.pagination .next_page').attr('href')
          if url && $(window).scrollTop() > $(document).height() - $(window).height() - 230
            $('.pagination').text("Načítavam...")
            $.getScript(url)
        $(window).scroll()

我从我的圣经中学到的 ;-) RailsCasts - Ryan Bates

于 2012-06-05T06:56:43.743 回答