Unexpected array output on page [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-12-11 11:20:08

问题


I'm listing a set of data (for simplicity sake, just the identity column) of a particular database table table as follows:

<%= @fields.select{|field| field.model=="PreferredOffering"}.each do |field| %>
    <%= field.id %>
    <br/>
<% end %>

As you may have gathered from above, I'm using the combination of select and each to iterate ONLY through rows whose column model contains the string PreferredOffering.

My expectation was that I would see a nice ordered list of numbers and indeed I do. My confusion is that I ALSO see the entire @fields array coughed all over the page, below the list of numbers. (See below html excerpt)

106
<br/>

107
<br/>

108
<br/>

109
<br/>

110
<br/>

111
<br/>

112
<br/>
[#&lt;PreferredOfferingField id: 5, field_heading: &quot;Anti-dilution provisions- Typical Weighted Average&quot;, category: &quot;Anti-Dilution&quot;, intra_cat_order: 1, model: &quot;P

My guess is that I'm doing something funny with select as I'm not really familiar with its usage.

Any ideas on how to remedy this would be received gratefully; thanks in advance.


回答1:


<% %> Executes the Ruby code inside

<%= %> Prints the results

You are displaying array and then it's values, so you would want to change <%= %> to <% %> .

<%= @fields.select{|field| field.model=="PreferredOffering"}.each do |field| %>

to

<% @fields.select{|field| field.model=="PreferredOffering"}.each do |field| %>


来源:https://stackoverflow.com/questions/30633707/unexpected-array-output-on-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!