2012-01-31 15 views
0

我在使用紅寶石表時遇到問題。我允許用戶通過前綴或標題來搜索類,以便將它們發送到found_by_prefix或found_by_title頁面,我需要使用find_all_by_prefix(params[:prefix])。所以當我這樣做的時候,我得到了一個錯誤,所以我研究了它,發現使用<%[email protected](&:prefix).join(', ')%>會把所有在我的表中找到的課程,但它只是把所有的結果放在一行。我只想知道如何爲4個結果製作4行!使用find_all_by Ruby時出錯ERROR

控制器:

def found_by_prefix 
     @course = Course.find_all_by_prefix(params[:prefix]) 
end 

查看:

<table border=1> 
<tr><th>id</th><th>prefix</th><th>number</th><th>title</th><th>section</th></tr> 
<tr> 
<td><%[email protected](&:id)%></td> 
<td><%[email protected](&:prefix).join(', ')%></td> 
<td><%[email protected](&:number).join(', ')%></td> 
<td><%[email protected](&:title).join(', ')%></td> 
<td><%[email protected](&:section).join(', ')%></td> 
</tr> 
</table> 
+0

閱讀:http://guides.rubyonrails.org/active_record_querying.html – shibly 2012-01-31 01:05:15

回答

0

通過的結果只是循環,從而爲每一個行:

<% @course.each do |course| %> 
    <tr> 
     <td><%= course.id %></td> 
     <td><%= course.prefix %></td> 
     <td><%= course.number %></td> 
     <td><%= course.title %></td> 
     <td><%= course.section %></td> 
    </tr> 
<% end %> 

此外,你應該命名實例變量@courses而不是@course,因爲它代表一個集合,而不是一個罪gle模型對象。

相關問題