2013-01-13 142 views
3

我正在关注Head First Rails,它是为老版本的rails编写的。我正在研究其第2章项目,但在Rails 3中。以下是我的文件。ActionView :: Template :: Error undefined方法

ads_controller.rb

class AdsController < ApplicationController 
    def show 
    @ad = Ad.find(params[:id]) 
    end 

    def index 
    @ads = Ad.find(:all) 
    end 
end 

ad.rb

class Ad < ActiveRecord::Base 
    attr_accessible :description, :email, :img_url, :name, :price, :seller_id 
end 

index.html.rb

</h1> 
<ul> 
<% for ad in @ads %> 
    <li><a href="/ads/<%= ad.id %>"><%= ad.name %></a></li> 
<% end %> 
</ul> 

show.html.rb

<body> 
    <p> 
     <b>Name:</b><%= @ad.name %> 
    </p> 
    <p> 
     <b>Description:</b><%= @ad.desciption %> 
    </p> 
    <p> 
     <b>Price:</b><%= @ad.price %> 
    </p> 
    <p> 
     <b>Seller Id:</b><%= @ad.seller_id %> 
    </p> 
    <p> 
     <b>Email:</b><%= @ad.email %> 
    </p> 
    <p> 
     <img src="<%= @ad.img_url %>"/> 
    </p> 
</body> 

schema.rb

ActiveRecord::Schema.define(:version => 20121209174120) do 

    create_table "ads", :force => true do |t| 
    t.string "name" 
    t.text  "description" 
    t.decimal "price" 
    t.integer "seller_id" 
    t.string "email" 
    t.string "img_url" 
    t.datetime "created_at", :null => false 
    t.datetime "updated_at", :null => false 
    end 

end 

数据库被填充的所有字段。 http://0.0.0.0:3000/ads/作品,但http://0.0.0.0:3000/ads/<any number>不起作用,引发以下错误:

NoMethodError在广告#显示

Showing /Users/ava/Projects/mebay/app/views/ads/show.html.erb where line #6 raised: 

undefined method `desciption' for #<Ad:0x007f7ff346c100> 

Extracted source (around line #6): 

3:  <b>Name:</b><%= @ad.name %> 
4: </p> 
5: <p> 
6:  <b>Description:</b><%= @ad.desciption %> 
7: </p> 
8: <p> 
9:  <b>Price:</b><%= @ad.price %> 

Rails.root: /Users/ava/Projects/mebay 
Application Trace | Framework Trace | Full Trace 

app/views/ads/show.html.erb:6:in `_app_views_ads_show_html_erb___4217851794431988162_70093760484140' 

在服务器窗口:

ActionView::Template::Error (undefined method `desciption' for #<Ad:0x007f7ff346c100>): 
    3:  <b>Name:</b><%= @ad.name %> 
    4: </p> 
    5: <p> 
    6:  <b>Description:</b><%= @ad.desciption %> 
    7: </p> 
    8: <p> 
    9:  <b>Price:</b><%= @ad.price %> 
    app/views/ads/show.html.erb:6:in `_app_views_ads_show_html_erb___4217851794431988162_70093760484140' 

如果我在show.html.rb删除说明那么它工作正常,并显示名称,价格,seller_id和电子邮件。我究竟做错了什么?

的Ruby版本:ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.4.0] Rails的版本:Rails 3.2.7

感谢。

+1

这是非常艰难的书作者/出版商使用Rails跟上,但对于学习使用本书和Rails版本中匹配的主要版本号的人来说非常重要。 Rails足够复杂,版本之间的差异可能会让一本书令人生畏。 –

+2

我可以推荐[Rails教程](http://ruby.railstutorial.org/ruby-on-rails-tutorial-book)吗? – sunnyrjuneja

回答

5

你错过了在 '描述' 的 'R' 在show.html.erb:

undefined method 'desciption'

<b>Description:</b><%= @ad.desciption %>

+0

Woah无法相信,即使经过多次检查,我仍然错过了它。谢谢。 – Ava

相关问题