我正在使用 Ruby 可枚举来从另一个模型创建数组。方法“companyattending”
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
奇怪的是,当我将其放入 View 中时,我得到了预期的项目列表,然后列表末尾的数组成员仍然在数组中:
查看结果
<ul style="list-style-type: none;">
<li>Company 1</li>
</ul>
<ul style="list-style-type: none;">
<li>Company 2</li>
</ul>
["3point5.com", "Company", "5.1", "A O Coolers", "Abo Gear", "Access Fund", "AceCamp.com", "ACORN"
/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
我错过了什么?
最佳答案
而不是:
<%= @accounts.each do |f|%>
在没有=
的情况下使用它:
<% @accounts.each do |f|%>
=
是您的代码显示数组的原因。
关于ruby-on-rails - Ruby 可枚举收集数组仍然显示在列表末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16651061/