3

古いバージョンのレール用に書かれたHead First Railsに従っています。私は第2章プロジェクトに取り組んでいますが、Rails 3.以下は私のファイルです。

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#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 の説明を削除すると、問題なく動作し、名前、価格、seller_id、および電子メールが表示されます。私は何を間違っていますか?

Ruby バージョン: ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.4.0] Rails バージョン:Rails 3.2.7

ありがとう。

4

1 に答える 1

6

show.html.erb の「説明」に「r」がありません:

undefined method 'desciption'

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

于 2013-01-13T02:31:28.553 に答える