0

我想在我的部分中使用两个集合 @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} %>
4

2 回答 2

0

请参阅传递局部变量

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

您可以在 yourads/ad_item 部分访问@ad_items作为ad_items@user作为用户

于 2013-02-01T12:29:05.980 回答
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 %>
于 2013-02-01T13:20:59.610 回答