Forum: Ruby on Rails using partials in a for loop

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7813d26c8ef483e08432ec9c574664d6?d=identicon&s=25 Ben (Guest)
on 2006-05-11 00:05
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:

<li>
<%= link_to_remote "#{course.title}",
    {:url => {:action => "add_course", :id => course}},
     :class    => "course", :name => "#{course.title}"
     %>
</li>

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.
Fb23bc8cd4030c526b0689276b34c8bd?d=identicon&s=25 Bryan Duxbury (bryanduxbury)
on 2006-05-11 00:13
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.
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-05-11 00:14
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:
>
> <li>
> <%= link_to_remote "#{course.title}",
>     {:url => {:action => "add_course", :id => course}},
>      :class    => "course", :name => "#{course.title}"
>      %>
> </li>
>
> 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 %>
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-05-11 00:16
Bryan Duxbury 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'
7813d26c8ef483e08432ec9c574664d6?d=identicon&s=25 Ben (Guest)
on 2006-05-11 01:50
Thanks guys, that got it going.
This topic is locked and can not be replied to.