我正在使用 mongoid 并且在生成普通脚手架后没有添加任何代码。
我想在投资组合索引视图页面上呈现类别列表。
所以我呈现为部分。但得到如下错误..
NoMethodError in Portfs#index
Showing /Users/suri/Dev/action2/app/views/cats/_list.html.erb where line #1 raised:
undefined method `each' for nil:NilClass
Extracted source (around line #1):
1: <% @cats.each do |cat| %>
2: <li><%= cat.name %></li>
3: <% end %>
查看/portf/index.html.erb
<div class="row">
<div class="span9">
<div class="row">
<ul class="thumbnails">
<% @portfs.each do |portf| %>
<li class="span3"style="border:1px solid black;">
<div class="thumbnail">
<% portf.images.each do |image| %>
<%= image_tag image.file.url(:small) %>
<% end %>
<div class="caption">
<h3><%= link_to (truncate portf.title, :length =>10), portf %></h3>
<p><%= truncate portf.decs, :length => 30 %></p>
<p><%= link_to 'Edit', edit_portf_path(portf) %>
<%= link_to 'Destroy', portf, method: :delete, data: { confirm: 'Are you sure?' } %></p>
</div>
</div>
</li>
<% end %>
</ul>
</div>
</div>
<div class="span3">
<div class="modal fade" id="portf_modal">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>Welcom to <%= image_tag 'logo.png', :height => 12 %></h3>
</div>
<div class="modal-body">
<%= form_for :portf, :html => { :multipart => true } do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<%= f.label :Category %><br />
<%= collection_select(:portf, :category, Cat.all, :name, :name) %>
<div class="field">
<%= f.label :decs %><br />
<%= f.text_area :decs, :class => 'span5', :rows => 10 %>
</div>
</div>
<div class="modal-footer">
<%= f.submit '올리기', :class => 'btn btn-primary' %>
<a href="#" class="btn" data-dismiss="modal">닫기</a>
<% end %>
</div>
</div>
<form class="form-search">
<div class="input-append">
<input type="text" class="span2 search-query">
<button type="submit" class="btn">Search</button>
</div>
</form>
<p>category</p>
<% render 'cats/list', :action => "index", :controller => "cats" %>
<a href="#portf_modal" data-toggle="modal" class="btn btn-primary">포트폴리오 올리기</a>
</div>
</div>
查看/cats/_list.html.erb
<% @cats.each do |cat| %>
<li><%= cat.name %></li>
<% end %>