2013-01-13 138 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

相關問題