Using partials in a for loop

I’m trying to write a partial for a link_to_remote that i’m using over
and over again, but simply moving the code to a partial doesn’t seem to
work.

Here is how I have it set up:

class_planner.rhtml:

<% for course in @courses %>
<%= render :partial => “class_list” %>
<% end %>

_class_list.rhtml:

  • <%= link_to_remote "#{course.title}", {:url => {:action => "add_course", :id => course}}, :class => "course", :name => "#{course.title}" %>
  • The main error I’m getting is that the method ‘course’ is not found.

    Is there something that I have to do differently when using a partial?
    I’m thinking that I can’t access the ‘course’ using #{course.whatever}
    in a partial, but I’m not sure how to access it.

    Any help would be much appreciated.

    Thanks.

    Your problem is that course isn’t in the scope of the partial. By
    default, the only variable that will get automatically passed is an @
    variable that has the same name as the partial itself. If you want to
    pass course into the partial, you need to amend your render call:

    render(:partial => “course_list”, :locals => {:course => course})

    And then, in your partial, @course should be available.

    Ben wrote:

    I’m trying to write a partial for a link_to_remote that i’m using over
    and over again, but simply moving the code to a partial doesn’t seem to
    work.

    Here is how I have it set up:

    class_planner.rhtml:

    <% for course in @courses %>
    <%= render :partial => “class_list” %>
    <% end %>

    _class_list.rhtml:

  • <%= link_to_remote "#{course.title}", {:url => {:action => "add_course", :id => course}}, :class => "course", :name => "#{course.title}" %>
  • The main error I’m getting is that the method ‘course’ is not found.

    Is there something that I have to do differently when using a partial?
    I’m thinking that I can’t access the ‘course’ using #{course.whatever}
    in a partial, but I’m not sure how to access it.

    Any help would be much appreciated.

    Thanks.

    Your local variable you set up before you call the partial are out of
    scope, and do not exist within the partial. Instance would exist, but
    you shouldn’t set instance variables in your view. The right is to tell
    partial to provide values for the local variables.

    <% for course in @courses %>
    <%= render :partial => “class_list”, :locals => {:course => course} %>
    <% end %>

    Bryan D. wrote:

    Your problem is that course isn’t in the scope of the partial. By
    default, the only variable that will get automatically passed is an @
    variable that has the same name as the partial itself. If you want to
    pass course into the partial, you need to amend your render call:

    render(:partial => “course_list”, :locals => {:course => course})

    And then, in your partial, @course should be available.

    local vars are created not instance vars, so you access it with ‘course’
    not ‘@course

    Thanks guys, that got it going.