0

I have a model students. in studentsController I select a collection of students. I want to attach a flag to the first and the last record.

I'll display the list of students with a partial like this: students/_student and when a user clicks each student, they'll get a popup.

I want to use the flag to determine whether this student is the first/last so that I can add a query parameter to the url. So for instance, the url will look like this when you hover over the first student: localhost:3000/student/1?location=first

When displaying though a partial, the collection is not present in the partial, and so I cannot just do a test like this: student.first?

Any ideas? Thank you.

[EDIT]. This was originally a reply to @neils, but I figured I'd put it out there: It works however I have another issue. Because I'm using an "auto-scroll" feature, which loads in more students as the user scrolls down the page ( 3 at a time), the "first" and "last" gets added to the first and third student of each row rather than the actual first and last of the collection. The way I do the auto-scroll: I use $.getScript to request the index.js.erb file. That file renders the students/_student like so: render @students, and attach to a container div. I render the url in that template (students/_student), so the params gets rendered every first/third time.

6
  • why don't you use params[:location] as you are passing it in url?? Commented Sep 4, 2012 at 18:37
  • Please add some code so that its easy to solve the problem. Commented Sep 4, 2012 at 18:43
  • @Sush I'm not sure I understand what you mean. I will use params[:location] to get it back and figure out whether it's the first or last item. My problem is attaching the location params to the url in the first place. I need to figure when to attach it, and so I need to know which item is the first and which one is the last. Commented Sep 4, 2012 at 18:57
  • @soundar I'm at the day job right now, so I can't paste in any code until later on. Is there something specific you want me to explain? Thanks for your response. Commented Sep 4, 2012 at 18:58
  • ok.Are you rendering the students like render "student", :collection => @students ? Commented Sep 4, 2012 at 19:07

3 Answers 3

2

You can proceed with your render @students call if you want. Just take note that you magically have a student_counter helper available for you in your partial when you do that. Now in your partial, you can compare the student_counter with @students.size and set your link_to's :location=>'first/last' via helper or decorator.

Sign up to request clarification or add additional context in comments.

1 Comment

I will definitely try this out. Thank you!
1

Assuming that you already assign some collection of students to @students (Student.all, Student.where(some_conditions), whatever...) in your controller, you could do the following:

Add attr_accessor :flag to your Student model:

class Student < ActiveRecord::Base
  attr_accessor :flag
  #... rest of your code
end

This creates an non-ActiveRecord attribute on you Student instances.

Than you could do something in the controller like:

@students[0].flag = 'first'
@students[-1].flag = 'last'

and then use that info in your view. Not really elegant, but it should do the trick.

EDIT: Elaborated the above a bit, to make it more clear. EDIT2: Fixed mistake, attr_accessor is the right method, not attr_accessable...

7 Comments

niels what you think @students[0] contents?
Well, it's elegant enough. Thanks @neils!
Just checked this out, but this wouldn't work. There's no flag method on @students.
@berto77, you would have to create an attribute for flag. You can't just assign it on the fly.
After a moment of consideration, I think my solution, although it should work, violates some best practices: it introduces view logic in the model and in the controller. So, use at your own risk! :)
|
0

I'm not sure if I understand your problem correctly, but I'm going to take a shot at it.

Assuming you're using @students as the instance variable for your list, you can pass the list of students to your partial like so:

<%= render 'students', :locals => {:students => @students} %>

Inside the students/_students, you can loop through the list and determine their position by using each_with_index:

<% students.each_with_index do |student, index| %>

5 Comments

Hi @Terence thanks for your answer. I'm trying to avoid doing a loop in my views. I'm trying to use the best practice like so: render :@students where the actual partial that gets rendered is students/_student for each record in @students; without the obvious loop. See here: rails-bestpractices.com/posts/61-simplify-render-in-views
@berto77, you said in your question that you want the collection to be present in your partial, but you're trying to avoid doing a loop in your views? Those two statements are contradictory to each other. What exactly are you trying to accomplish? Also, the point of doing render 'students', :locals => {:students => @students} (which isn't necessarily bad practice according to your link) instead of render @students is to use each_with_index. Doing the second option will only result to using each.
you can proceed with your 'render @students" call if you want. just take note that you magically have a student_counter helper available for you in your partial when you do that. now in your partial, you can compare the student_counter with @students.size and set your link_to's :location=>'first/last' via helper or decorator
I wasn't aware of that helper. Thanks. TIL. You should probably post that as an answer.
@keikun17 Thanks! This makes total sense. I'll try it out after 5 p.m :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.