当使用<%= @questions %>显示问题表中的所有问题时,我将在视图#<ActiveRecord::Relation::ActiveRecord_Relation_Question:0x00000103644cc8>中得到以下内容。
在控制器中,我有:
@questions = Question.all
我把桌子上的问题显示出来有什么不对?
发布于 2014-10-20 15:46:11
您需要在任何HTML结构中循环遍历问题和输出。
例如,作为无序列表:
<ul>
<% @questions.each do |question| %>
<li><%= question.title %></li>
<% end %>
</ul>发布于 2014-10-20 15:49:27
要在索引页上列出问题,代码应该如下所示:
<table>
<tr>
<th>Title</th>
<th>Description</th>
</tr>
<% @questions.each do |question| %>
<tr>
<td><%= question.title %></td>
<td><%= question.description %></td>
</tr>
<% end %>
</table>发布于 2014-10-20 15:50:11
@questions是一个集合,您需要在视图中迭代该集合,或者将其传递给名为_question.html.erb的部分集合。要迭代,可以这样做:
<% @questions.each do |question| %>
<p>question.title</p>
<% end %>否则,如果创建一个名为_question.html.erb的部分,如下所示:
<p><%= question.title %><p>在您的主视图中,您可以将@questions对象传递给像这样的部分,它将打印@questions集合的所有成员
<% render @questions %>https://stackoverflow.com/questions/26469496
复制相似问题