我是铁轨上的红宝石的新手。我正在尝试创建一个在类别下有子类别的应用程序,但我不断收到错误: 子类别中的NoMethodError #index
显示C:... / app / views / subcategories / index.html.erb第16行:
nil的未定义方法`name':NilClass 提取的来源(第16行):
13: <% @subcategories.each do |subcategory| %>
14: <tr>
15: <td><%= subcategory.name %></td>
16: <td><%= subcategory.category.name %></td>
17: <td><%= link_to 'Show', subcategory %></td>
18: <td><%= link_to 'Edit', edit_subcategory_path(subcategory) %></td>
19: <td><%= link_to 'Destroy', subcategory, confirm: 'Are you sure?', method: :delete %></td>
这些是我的文件结构:
subcategories_controller.rb
class SubcategoriesController < ApplicationController
before_filter :login_required, :only => [:edit, :update, :new, :create, :index, :destroy]
# GET /subcategories
# GET /subcategories.json
def index
@subcategories = Subcategory.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @subcategories }
end
end
# GET /subcategories/1
# GET /subcategories/1.json
def show
@subcategory = Subcategory.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @subcategory }
end
end
# GET /subcategories/new
# GET /subcategories/new.json
def new
@subcategory = Subcategory.new
@categories = Category.all
respond_to do |format|
format.html # new.html.erb
format.json { render json: @subcategory }
end
end
# GET /subcategories/1/edit
def edit
@subcategory = Subcategory.find(params[:id])
@categories = Category.all
end
# POST /subcategories
# POST /subcategories.json
def create
@subcategory = Subcategory.new(params[:subcategory])
respond_to do |format|
if @subcategory.save
format.html { redirect_to @subcategory, notice: 'Subcategory was successfully created.' }
format.json { render json: @subcategory, status: :created, location: @subcategory }
else
format.html { render action: "new" }
format.json { render json: @subcategory.errors, status: :unprocessable_entity }
end
end
end
# PUT /subcategories/1
# PUT /subcategories/1.json
def update
@subcategory = Subcategory.find(params[:id])
respond_to do |format|
if @subcategory.update_attributes(params[:subcategory])
format.html { redirect_to @subcategory, notice: 'Subcategory was successfully updated.' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: @subcategory.errors, status: :unprocessable_entity }
end
end
end
# DELETE /subcategories/1
# DELETE /subcategories/1.json
def destroy
@subcategory = Subcategory.find(params[:id])
@subcategory.destroy
respond_to do |format|
format.html { redirect_to subcategories_url }
format.json { head :ok }
end
end
end
_form.html:
<%= form_for(@subcategory) do |f| %>
<% if @subcategory.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@subcategory.errors.count, "error") %> prohibited this subcategory from being saved:</h2>
<ul>
<% @subcategory.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :category_id %><br />
<%= f.collection_select :category_id, Category.all(:order => "name"), :id, :name,:prompt => "-- Select a Category --"%>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
index.html.erb
<h1>My subcategories</h1>
<h4><a href="categories">Category</a> <a href="my_account">My Account</a> <a href="log_out">Log out</a></h4>
<table>
<tr>
<th>Name</th>
<th>Category</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @subcategories.each do |subcategory| %>
<tr>
<td><%= subcategory.name %></td>
<td><%= subcategory.category.name %></td>
<td><%= link_to 'Show', subcategory %></td>
<td><%= link_to 'Edit', edit_subcategory_path(subcategory) %></td>
<td><%= link_to 'Destroy', subcategory, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Subcategory', new_subcategory_path %>
谢谢你们......
答案 0 :(得分:1)
您的子类别类别为零。使用db约束修复它,不允许在category_id字段中使用null,或者像这样检查视图:
<td><%= subcategory.category.name if subcategory.category %></td>