2012-12-10 28 views
0

我有这个问题的时间太长了,我不能找出问题出在哪里,所以:未定义的方法名称:NilClass

Showing /home/alex/Desktop/personal/app/views/entries/list.html.erb where line #17 raised: 

    undefined method `name' for nil:NilClass 

Extracted source (around line #17): 

14: <% @entries.each do |entry| %> 
15: <tr> 
16: <td><%= link_to entry.title, :action => "show", :id => entry.id %></td> 
17: <td><%= link_to entry.category.name, :action => "list", :category_id => entry.category.id %></td> 
18: </tr> 
19: <% end %> 
20: 

我的意见/项/ list.html.erb外表看起来是这样的:

<html> 
    <head> 
    <title>All Entries</title> 
    </head> 
    <body> 

    <h1>Online Personal Collection- All Entries</h1> 
    <table border="1"> 
    <tr> 
    <td width="80%"><p align="center"><i><b>Entry</b></i></td> 
    <td width="20%"><p align="center"><i><b>Category</b></i></td> 
    </tr> 

    <% @entries.each do |entry| %> 
    <tr> 
    <td><%= link_to entry.title, :action => "show", :id => entry.id %></td> 
    <td><%= link_to entry.category.name, :action => "list", :category_id => entry.category.id %></td> 
    </tr> 
    <% end %> 


    </table> 
    <p><%= link_to "Create new entry", :action => "new" %></p> 
    <br /> 
    <%=link_to "Back to Index", entries_path%> 
    <br /> 
    <%=link_to "Back to Account Info", my_account_path%> 
    <br /> 
    <h3>Enter keyword</h3> 
<form action ="search" method="post"> 
<input name = "key" type="input" /> 
<input value="Send" type="submit"/> 
</form> 
</body> 
</html> 

的模型是这样的:

class Entry < ActiveRecord::Base 
    attr_accessible :comments, :date, :description, :title, :category_id, :category_name 
    belongs_to :category 
after_create do |entry| 
     logger.info "entry created: #{entry.title} #{entry.description}" 
end 
end 




class Category < ActiveRecord::Base 
    attr_accessible :name 
    has_many :entries 
end 

而且entries_controller:

class EntriesController < ApplicationController 
    # GET /entries 
    # GET /entries.json 
    def index 

    @entries = Entry.all 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @entries } 
    end 
    end 

    def list 
     if params[ :category_id].nil? 
        @entries = Entry.find(:all) 
      else 
       @entries = Entry.find(:all , 
           :conditions => ["category_id = ?" , params[ :category_id]]) 
        params[ :category_id] = nil 

     respond_to do |format| 
     format.html # list.html.erb 
     format.json { render json: @entry } 
    end 
    end 
    end 


    # GET /entries/1 
    # GET /entries/1.json 
    def show 
    @entry = Entry.find(params[:id]) 
    @category = Category.find(:all) 


    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @entry } 
    end 
    end 

    # GET /entries/new 
    # GET /entries/new.json 
    def new 
    @entry = Entry.new 
    @categories= Category.find(:all) 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @entry } 
    end 
    end 

    # GET /entries/1/edit 
    def edit 
    @entry = Entry.find(params[:id]) 
    @categories = Category.find(:all) 
    end 

    # POST /entries 
    # POST /entries.json 
    def create 
    @entry = Entry.new(params[:entry]) 
    respond_to do |format| 
     if @entry.save 
     format.html { redirect_to @entry, notice: 'Entry was successfully created.' } 
     format.json { render json: @entry, status: :created, location: @entry } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @entry.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /entries/1 
    # PUT /entries/1.json 

现在,如果有人发现问题,并可以帮助我了解我在做错的地方,我将不胜感激。 谢谢! Alex。

回答

2

你有一个没有分类的条目,所以entry.category是零,所以entry.category.name(在这种情况下)是nil.name,这是没有意义的。由于这个问题,避免这种关联的链接方法通常是很好的做法。

的Rails内置了代表团,你可以用它来防止这种情况:

class Entry < ActiveRecord::Base 
    #... 
    delegate :name, to: :category, prefix: :category, allow_nil: true 
end 

这定义了entry.category_name实例方法。在您看来,如果该条目不存在任何类别,则不会出现该条目。您可以阅读关于委托方法和选项here的更多信息。

UPDATE:

所以我忽略了你要链接到零对象,事实可能是,当你真正想做的事,当对象是无没有显示链接的。有一个link_to_if方法(即我从来没有用过,但可能会为你工作):

<%= link_to_if(entry.category, entry.category_name, :action => "list", :category_id => entry.category.id %> 

您仍然需要使用category_name委托方法,因为这个名字被打印(不链接),当第一参数评估为false。

+0

欣赏,但要么我不明白你对我说或不工作。我得到这个:被调用的ID为零,这将错误地为4 - 如果你真的想要的ID为零,请使用object_id提取的源代码(在第17行左右):16:​​<%= link_to entry.title,:action = >“show”,:id => entry.id%> 17:​​<%= link_to entry.category_name,:action =>“list”,:category_id => entry.category.id%> 18: 19 :<% end %>我改变了模型,如你所说和这个:​​<%= link_to entry.category_name,:action =>“list”,:category_id => entry.category.id%> –

+0

那么,你会得到相同的现在出错了,但在'entry.category.id'上。一分钟,我会看看我是否可以编辑我的答案以提供更多帮助。 –

+0

应该有额外的结束权利?因为它给我一个语法错误: /home/alex/Desktop/personal/app/views/entries/list.html.erb:20:语法错误,意外的关键字结束,期待')' '); (第20行左右): 17:​​<%= link_to_if(entry.category,entry。CATEGORY_NAME,:动作=> “清单”,:CATEGORY_ID => entry.category.id%> 18: 19: 20:<% end %> 21: 22: 23: –

相关问题