jeudi 27 août 2015

Rails 3 head first rails Mebay application error

I am using rails 3 and learning it fron head first rails book. I am in chapter 2 creating Mebay application with ads table.

The steps that I did are

1.rails new MeBay

2.cd to MeBay directory

3.rails g model ad name:string description:text price:decimal seller_id:integer email:string imr_url:string

4.rake db:migrate

5.rails generate controller ads

6.show.html.erb created in folder app/views/ads as below.

 <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>

  1. config/routes.rb file

MeBay::Application.routes.draw do controller 'ads' do match 'ads/:id' => :show end end

When I started rails server and in browser

http://locathost:3000/ads/3

I only see labels with empty values. in other words, I don't see values next to the labels as the book says on page 63.

so far going good.

page 65 says we need to tell the model to read the record from the ads table in the database. But I have created only the ads table I don't have values entered in database for this application like page 65 shows an example.

When I don't have data, I added the code like page 69 says in views/ads/show.html.erb as

 <html>
    <head>
      <title> </title> 
    </head>
    <body>
    <p>
      <b>Name:</b><%= @ad.name %>
    </p>
    <p>
     <b>Description:</b><%= @ad.description %>
    </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>
    </html>

My test drive on this code displayed error

NoMethodError in Ads#show

Showing C:/Ruby193/RailsProject/MeBay/app/views/ads/show.html.erb where line #7 raised:

undefined method `name' for nil:NilClass Extracted source (around line #7):

4: </head>
5: <body>
6: <p>
7:   <b>Name:</b>><%= @ad.name %>
8: </p>
9: <p>
10:  <b>Description:</b><%= @ad.description %>

Please help on this. I have searched all the forums and worked on hours on this problem. Thank you for the help.

Aucun commentaire:

Enregistrer un commentaire