Ruby On Rails:渲染集合

时间:2011-11-12 02:42:33

标签: ruby-on-rails

我希望在调用部分模板时在第一次迭代时显示特定图像,而不是其余部分:

view file:

<% @categories.each do |category,i,category_locales| %>
    <div class="featured_deals">
        <%= render :partial => "medium_deal", :collection => category_locales, :as => :deal_locale %>
      </div>
<% end %>

medium_deal文件:

<% deal = deal_locale.deal %>

<%= image_tag 'layout/featured_deal_left_blue.png', :style => 'float: left; padding-top: 11px;' %> 
# I only want this image to show for the FIRST element of category_locales, but not the rest.

<div class="featured_deal_wrapper">
        Hello     
</div> 

我尝试在第一个视图文件中传递一个计数器,但是直到&#34;每个&#34;再次通过。

1 个答案:

答案 0 :(得分:0)

你应该可以这样做:

<% if deal_locale_counter == 0 %>
  <%= image_tag 'layout/featured_deal_left_blue.png', :style => 'float: left; padding-top: 11px;' %> 
<% end %>

请参阅:http://guides.rubyonrails.org/layouts_and_rendering.html#local-variables