如何在rails中使用具有多个集合的partials

时间:2013-02-01 12:08:09

标签: ruby-on-rails ruby ruby-on-rails-3 erb

我想在我的部分中使用两个集合@ad_item和@user。这是我的索引erb ...

<% if @ad_items.any? && @user.any? %>
<%= render partial: 'yourads/ad_item', collection: {@ad_items,@user} %>
<% end %>

这是我的控制器......

def index
  @ad_items = Yourad.all
  @user = User.all
  respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @yourads }
end

这是我的_ad_item.erb.html部分

     <span class="date"><%= ad_item.created_at.to_date() %></span>
     <span class="location"><%= ad_item.title %></span>
     <div style="float:left"><%= ad_pic @user,ad_item %></div>
     <div class="description"><%= ad_item.description %></div>

我的助手功能是..

def ad_pic(user,ad)
cl_image_tag("Ad#{user.id}#{ad.id}.jpg", :version => rand(1000000000), :alt => "Ad pic",:width => 70, :height => 70, :crop => :fill)
end

它在..

中出现语法错误
<%= render partial: 'yourads/ad_item', collection: {@ad_items,@user} %>

2 个答案:

答案 0 :(得分:0)

请参阅Passing Local Variables

<%= render partial: 'yourads/ad_item', :locals => {:ad_items=>@ad_items,:user => @user }%>

您可以在yourads / ad_item partial中以 ad_items @user 作为用户访问 @ad_items < / p>

答案 1 :(得分:0)

<%= render partial: 'yourads/ad_item', collection: @ad_items , 
                                       locals: {users: @users} %>

并在视野中

<% for user in users %>
 <span class="date"><%= ad_item.created_at.to_date() %></span>
 <span class="location"><%= ad_item.title %></span>
 <div style="float:left"><%= ad_pic user,ad_item %></div>
 <div class="description"><%= ad_item.description %></div>
<% end %>