1

我是 ruby​​ on rails 的新手。我正在尝试创建一个在类别下具有子类别的应用程序,但我不断收到错误消息:NoMethodError in Subcategories#index

显示 C:.../app/views/subcategories/index.html.erb 其中第 16 行提出:

nil 的未定义方法“名称”: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 %>

多谢你们...

4

1 回答 1

1

您的子类别的类别为零。使用不允许在 category_id 字段中为 null 的 db 约束来修复它,或者像这样检查视图:

<td><%= subcategory.category.name if subcategory.category %></td>
于 2012-12-14T15:56:45.710 回答