当我尝试运行Rails 3应用程序时显示> B
col1
1 a
2 c
3 e
4 f
5 x
6 y
:
广告#show中的NoMethodError
显示C:/Ruby193/RailsProject/MeBay/app/views/ads/show.html.erb 第7行提出的地方:
nil的未定义方法`name':NilClass提取的源(第7行左右):
NoMethodError
我正在“Head First Rails”一书的第2章中复制4: </head>
5: <body>
6: <p>
7: <b>Name:</b>><%= @ad.name %>
8: </p>
9: <p>
10: <b>Description:</b><%= @ad.description %>
应用广告表
在命令行中,这就是我所做的:
Mebay
此时,$ rails new MeBay
$ cd to MeBay directory
$ rails g model ad name:string description:text price:decimal seller_id:integer email:string imr_url:string
$ rake db:migrate
$ rails generate controller ads
文件已在文件夹show.html.erb
中创建,其代码如下:
app/views/ads
这是我的 <html>
<head>
<title> </title>
</head>
<body>
<p>
<b>Name:</b>
</p>
<p>
<b>Description:</b>
</p>
<p>
<b>Price:</b>
</p>
<p>
<b>Seller Id:</b>
</p>
<p>
<b>Email:</b>
</p>
</body>
</html>
文件:
config/routes.rb
当我启动Rails服务器并在浏览器中加载页面(MeBay::Application.routes.draw do
controller 'ads' do
match 'ads/:id' => :show
end
end
)时,我只看到带有空值的标签。我没有看到标签旁边的值,因为书中指出应该在第63页上发生。
第65页表明应用程序应该告诉模型从数据库中的ads表读取记录。但我只创建了广告表,我没有为此应用程序在数据库中输入的值,如第65页示例所示。
当我没有数据时,我在http://locathost:3000/ads/3
中添加了代码,如第69页所示:
views/ads/show.html.erb
由于上述错误,我陷入了困境。请帮助解决此错误或在何处/如何在表格中输入数据。我已经搜索了所有论坛并处理了这个错误数小时。谢谢你的帮助。
答案 0 :(得分:0)
如果您在阅读本书时注意了,您会注意到,在第51页,他们会告诉您从此处下载应用所需的种子数据:http://www.headfirstlabs.com/books/hfrails/。
执行此操作,然后重试,它会起作用。