【发布时间】:2012-11-11 16:11:12
【问题描述】:
我正在关注一本名为 Rails Solutions: Rails Made Easy 的书,它是为 rails 2 编写的,但我使用的是 rails 3,这让事情变得非常有趣,让我学到了很多东西,这很好,但我我坚持上述问题。我在 Stack 和其他网站上阅读过,我认为这可能是路由问题,但到目前为止,这本书还没有提到任何关于路由的内容。
routes.rb
List::Application.routes.draw do
match ':controller(/:action(/:id))(.:format)'
end
app/views/classified/show.html.erb
<p>
<strong>Title: </strong> <%= @classified.title %><br />
</p>
app/controllers/classified_controller.rb
class ClassifiedController < ApplicationController
def list
@classifieds = Classified.find(:all)
end
def show
@classifieds = Classified.find(params[:id])
end
def new
@classified = Classified.new
end
def create
@classified = Classified.new(params[:classified])
if @classified.save
redirect_to :action => 'list'
else
render :action => 'new'
end
end
def edit
end
def update
end
def delete
end
end
Classified#show 中的 NoMethodError
显示 /home/mark/Documents/RoR/list/app/views/classified/show.html.erb 其中第 3 行出现:
nil:NilClass 的未定义方法 `title' 提取的源代码(第 3 行附近):
1: 2:
3:标题:
4:
【问题讨论】:
-
你会经常遇到这个错误。
undefined method 'title' for nil:NilClass表示您调用title的对象为零。在这里,它是@classified,在 Tomdarkness 的回答中了解原因
标签: ruby-on-rails ruby routes