2012-12-14 17 views
1

我是一個總是新手到紅寶石軌道上。我試圖創建一個在類別下有子類別的應用程序,但我不斷收到錯誤: 子類別中的NoMethodError#index子類別中的NoMethodError#索引

顯示C:.../app/views/subcategories/index.html.erb其中線#16提出:

未定義的方法`名」的零: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 %> 

感謝球員...

回答

1

你的子類別的類別是零。修正它與數據庫的限制,不允許null在category_id字段,或檢查在這樣的看法:

​​
+0

請我不明白。哪個文件應該保存上面的代碼行? –

+0

在'index.html.erb'中,#16行與此類似,因此切換至此。 – Matzi

+0

有條件的視圖渲染只能通過使錯誤消息消失來緩解症狀。潛在的問題仍然存在:沒有父類別的孤兒子類別。上述數據庫約束或[模型驗證](http://guides.rubyonrails.org/active_record_validations_callbacks.html)將有助於解決此問題。 – Substantial

相關問題