ActionView::Template::Error 未定义的方法

3

我正在学习《Head First Rails》,但该书是针对较旧版本的Rails写的。我正在用Rails 3完成第2章的项目。以下是我的文件。

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/<任意数字>无法使用,并显示以下错误:

NoMethodError in Ads#show

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中删除了描述,那么它就能正常工作并显示名称、价格、卖家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已经够复杂了,版本之间的差异可能会使一本书变得非常混乱。 - the Tin Man
2
我能推荐《Rails教程》吗? - sunnyrjuneja
1个回答

6
在show.html.erb中,您的'description'拼写错误,应该加上'r':

undefined method 'desciption'

<b>描述:</b><%= @ad.description %>


哇,即使检查了那么多次,我还是错过了它。谢谢。 - Ava

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接