Not signed in (Sign In)

SkillShare - A place to discuss Web Standards and Web Design topics

Categories

Vanilla 1.1.9 is a product of Lussumo. More Information: Documentation, Community Support.

    • CommentAuthorvarland
    • CommentTimeJan 5th 2007 edited
     permalink

    I have a Rails question for anybody who can help...

    I have a database table of faculty members for a school. The table has the following columns:

    • id
    • first_name
    • last_name
    • salutation
    • suffix
    • years_teaching

    I'm trying to render a template to show details about each faculty member on a page. To do so, I tried the following:

    <%= render :partial => 'list_entry', :collection => @faculty_members %>

    According to the Rails documentation, using the :collection property should force the partial to be rendered for each object in the collection. Each time the partial is rendered, a local variable whose name is the singular version of the collection name is created.

    To make sure everything was working, I created a simple partial containing the following:

    <li><%= faculty_member.last_name %></li>

    Of course, it didn't work. After toying around for a while, I got this to work:

    <% @faculty_members.each do |f| %>
    <%= render :partial => 'list_entry', :locals => { :faculty_member => f } %>
    <% end %>

    As far as I can tell, these two commands should be doing the same thing. Am I missing something?

    • CommentAuthorkyko
    • CommentTimeMar 12th 2007
     permalink

    if you are doing:

    <%= render :partial => 'foo', :collection => @bars %>
    

    Then your erb partial code should be:

    <li><%= foo.last_name %></li>
    

    not bar.last_name. When you render a partial through a collection, the model fields are made available in a local var with the same name as your partial.

    to quote the rails docs:

      # Renders a collection of the same partial by making each element
      # of @winners available through the local variable "person" as it
      # builds the complete response.
      render :partial => "person", :collection => @winners
    
    • CommentAuthorvarland
    • CommentTimeMar 14th 2007
     permalink
    kyko... Thanks for replying. I should have come back and closed this, because I figured this out a while ago. Since I figured it out, I can't imagine it working any other way.
Add your comments
    Username Password
  • Format comments as (Help)