2013-05-20 65 views
2

我使用Ruby enumerable來創建另一個模型的數組。該方法「companiesattending」Ruby Enumerable收集數組仍然顯示在列表末尾

class Conference < ActiveRecord::Base 
    has_many :accountconferences 
    has_many :accounts, :through => :accountconferences 
    accepts_nested_attributes_for :accounts 

    def companiesattending 
    accounts.collect {|f| f.name } 
    end 

end 

有什麼奇怪的是,當我在一個數組把這個變成一個觀點,我得到的項目清單預期,然後一些的數組成員在列表的末尾還:

查看結果

<ul style="list-style-type: none;"> 
     <li>Company 1</li> 
    </ul> 

    <ul style="list-style-type: none;"> 
     <li>Company 2</li> 
    </ul> 
[&quot;3point5.com&quot;, &quot;Company&quot;, &quot;5.1&quot;, &quot;A O Coolers&quot;, &quot;Abo Gear&quot;, &quot;Access Fund&quot;, &quot;AceCamp.com&quot;, &quot;ACORN&quot; 

/app/views/conferences/_accounts.html.erb

<div class="span5"> 
<h3 class="pull-left">Companies That Are Attending</h3></br></br></br> 
<%= @accounts.each do |f|%> 
    <ul style="list-style-type: none;"> 
     <li><%= f %></li> 
    </ul> 
<% end %> 
</div> 

/app/models/conferences.rb(顯示動作)

def show 
    @conference = Conference.find(params[:id]) 
    @accounts = @conference.companiesattending 
    end 

我缺少什麼?

回答

2

相反的:

<%= @accounts.each do |f|%> 

使用此不=

<% @accounts.each do |f|%> 

=是你的代碼顯示陣列的原因。

+0

工作。謝謝。 – DaveG

1

each返回self,即它被調用的對象,並且<%=顯示其內部的表達式求值的任何值。 <%=裏面的表達式是@accounts.each,它返回@accounts,ergo,@accounts被顯示。

如果你不想顯示的陣列做,用<%執行但不顯示代碼。