好的,到目前为止,我创建了一个新的控制器:
rails g controller home settings
在 settings.html.erb 我想显示一个部分,所以我只是添加了一行:
<%= render "categories/index", :locals => {:categories => @categories} %>
所以我的 categories/_index.html.erb 看起来像这样,并且有效:
<% for category in @categories do %>
<tr>
<td><%= category.typ %></td>
<td><%= link_to "Löschen", category,:class => 'btn btn-mini btn-danger', method: :delete, data: { confirm: 'Sind sie sicher?' } %></td>
</tr>
<% end %>
<tr>
<%= form_for Category.new do |f| %>
<td> <%= f.text_field :typ, :class => "input-small" %></td>
<td><%= f.submit "Speichern", :class => 'btn btn-mini btn-success' %></td>
</tr>
<% end %>
</table>
</div>
但是现在当我打开 localhost:3000/home/settings 时出现错误:
主页#settings 中的 NoMethodError
显示 C:/Sites/rublesql/app/views/categories/_index.html.erb 其中第 10 行提出:
nil:NilClass 的未定义方法 `each' 提取的源代码(第 10 行附近):
7: <th></th>
8: </tr>
9:
10: <% for category in @categories do %>
11: <tr>
12: <td><%= category.typ %></td>
所以我的问题是我做错了什么?
类别控制器:
class CategoriesController < ApplicationController
def index
@categories = Category.all
end
def destroy
@category = Category.find(params[:id])
@category.destroy
redirect_to categories_path
end
def create
@category = Category.new(params[:category])
@category.save
redirect_to categories_path
end
end