Agile web development with rails 2.0

hi,
I just bought this book and it gives the url
http://media.pragprog.com/titles/rails2/code/
for its source code.
Nothing works. I have been through the app in chapter 6 three times and
i get an error as soon as i implement the code form this url on page 79.

the error usually happens
with ‘rake db:migrate ’
and consists of
‘undefined method `image_url’ for #Product:0x31823a8
either in the browser or in the command line.

What gives!?
Most of the url’s in the book are 404 errors and it was published less
than 5-6 months ago, i know its open source but what the hell? is anyone
using this as a source or am i missing something?

I’m sure I cant be the first to buy this book, spend the time getting
everything running, working around the inaccuracies in the text, and
still not getting through the first application…

can someone please help with this book.
…one cant sift through the errata pages on the rails site because they
are set up so terribly.
how are people learning rails if the book doesn’t work?
are there corrections in the 2.1 pdf book that update the links?
why is this book advertised as the ‘de facto’ manufacturer-written book
when it seems like there could easily be more stable sources for free on
the web?
where are people going to learn rails for real-life apps not just
geeking out in forums?

any light shed would be very helpful

-tbnt

Cameron Cameron wrote:

the error usually happens
with ‘rake db:migrate ’
and consists of
‘undefined method `image_url’ for #Product:0x31823a8
either in the browser or in the command line.

This error will occur if you have not entered the migration code on page
66 properly (and run a rake db:migrate). It’s complaining that there is
no method called ‘image_url’ in the product model which there would be
if the database had a column called ‘image_url’ in the products table,
which it would have if the code on page 66 has been run.

I thought that book was awful too, but for different reasons. The index
is appalling, concepts are missing, dozens of questions are answered in
inappropriate places or not at all. On the other hand, I learned from it
and still use it as a reference from time to time.

Rails api documentation is pretty awful too. Fortunately rails itself is
extremely useful.

steve

if you’re looking for a book on real-life rails programming,
take a look at Practical Rails Social Networking
Sites,
http://www.amazon.com/Practical-Rails-Social-Networking-Sites/dp/1590598415

it creates a fully functional application (that is actually running at
http://railscoders.net/)

it’s pretty good,
covers the basics, plus details deployment.

hi,

my code in the 001_create products.rb file from page 66 looks exactly
like it does in the book and from the url
http://media.pragprog.com/titles/rails2/code/depot_c/db/migrate/001_create_products.rb
it is:
class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.column :title, :string
t.column :description, :text
t.column :image_url, :string
end
end

def self.down
drop_table :products
end
end
rake db:migrate says
(in /Users/Cameron/Sites/depot)
then back to the command line prompt
the error in the browser when add new product button is pressed is:
undefined method `image_url’ for #Product:0x31ffce0
RAILS_ROOT: script/…/config/…

the request is:
{“commit”=>“Create”, “product”=>{“price”=>“04.44”, “title”=>“lkjlkj”,
“description”=>“lkjlk”, “img_url”=>"/images/svn.jpg"}}

the response header is:
{“cookie”=>[], “Cache-Control”=>“no-cache”}

i think that even if i copy the code entirely from the provided url it
is still not going to work

any idea what the problem still might be?
thanks
-tbnt

Ok,
two questions,

your params are returning “img_url”, not “image_url”,

and try working in ruby script/console

p = Product.create(:title => “title”, :description => “something”,
:image_url => “some url”)

does that work

p = Product.find(:first).attributes ?
do you get an image_url key in there?

you’ve definitely run the migration?

class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.column :title, :string
t.column :description, :text
t.column :image_url, :string
end
end

undefined method `image_url’ for #Product:0x31ffce0

the request is:
{“commit”=>“Create”, “product”=>{“price”=>“04.44”, “title”=>“lkjlkj”,
“description”=>“lkjlk”, “img_url”=>"/images/svn.jpg"}}

your params are returning “img_url”, not “image_url”,

i fixed this, it was in _form.rhtml and i didn’t put it there…

and try working in ruby script/console

p = Product.create(:title => “title”, :description => “something”,
:image_url => “some url”)

i ran ruby script/console and typed in that code which returns:

p = Product.create(:title => “title”, :description => “something”, :image_url => “/images/svn.jpg”)
NoMethodError: undefined method image_url=' for #<Product:0x34479a8> from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1857:inmethod_missing’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1672:in
send' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1672:inattributes=’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1671:in
each' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1671:inattributes=’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1505:in
initialize_without_callbacks' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/callbacks.rb:225:ininitialize’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:449:in
new' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:449:increate’
from (irb):13

does that work

it throws the same error and does not ad an item

p = Product.find(:first).attributes ?

p = Product.find(:first).attributes
NoMethodError: You have a nil object when you didn’t expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.attributes
from (irb):14

do you get an image_url key in there?

do you mean switching “image url” in your code to an actual url
("/images/svn.jpg")?

you’ve definitely run the migration?

i ran:
ruby script/generate migration add_test_data
with the following results:

ruby script/generate migration add_test_data
exists db/migrate
Another migration is already named add_test_data:
db/migrate/003_add_test_data.rb

rake db:migrate, however no longer shows any errors.

but none of the test data loaded and when i click new product button the
following error is in the browser:

NoMethodError in Admin#new

Showing app/views/admin/_form.rhtml where line #11 raised:

undefined method `image_url’ for #Product:0x3362b3c

Extracted source (around line #11):

8: <%= text_area ‘product’, ‘description’ %>


9:
10:

Image url

11: <%= text_field ‘product’, ‘image_url’ %>


12:
13:

Price

14: <%= text_field ‘product’, ‘price’ %>

Cameron Cameron wrote:

hi,
I just bought this book and it gives the url
http://media.pragprog.com/titles/rails2/code/
for its source code.
Nothing works. I have been through the app in chapter 6 three times and
i get an error as soon as i implement the code form this url on page 79.

the error usually happens
with ‘rake db:migrate ’
and consists of
‘undefined method `image_url’ for #Product:0x31823a8
either in the browser or in the command line.

What gives!?
Most of the url’s in the book are 404 errors and it was published less
than 5-6 months ago, i know its open source but what the hell? is anyone
using this as a source or am i missing something?

I’m sure I cant be the first to buy this book, spend the time getting
everything running, working around the inaccuracies in the text, and
still not getting through the first application…

can someone please help with this book.
…one cant sift through the errata pages on the rails site because they
are set up so terribly.
how are people learning rails if the book doesn’t work?
are there corrections in the 2.1 pdf book that update the links?
why is this book advertised as the ‘de facto’ manufacturer-written book
when it seems like there could easily be more stable sources for free on
the web?
where are people going to learn rails for real-life apps not just
geeking out in forums?

any light shed would be very helpful

-tbnt

Hi Cameron,

I had the same rake abort error. I spent 24 hours trying to find a fix.
I’ve now ditched the Agile Web D. book and trying another book
“Beginning Ruby on Rails”. If there is a solution, please let me know.

Best, Julian Bourne

We use this book as a training manual for new developers and have had no
problems with the content. In fact, one developer raves that it is the
best
book he’s ever read when it comes to programming books.

I would suggest you take a second look at your code and make sure you’ve
implemented it just as the book instructs.

The NoMethodErrorr for ‘image_url’ you see is an indication that rake
db:migrate did not work. Check your database for the tables and
columns.

Hi Cameron and Julian, I guess one of the goals of the book is that you
learn by doing. This also includes learning how to interpret the error
messages in the browser as well as the logs. Next, you’re being overly
sensitive about the book and its content and you need to start checking
your
work before taking shots at the book in the forum. This is called
professional courtesy We, the Rails community, are here to assist you
but
you need to address this forum in a more tactful manner. Now, where do
you
stand with this issue?
a) Let’s check the database. Start the MySQL client.

mysql>  use <database_name>;

mysql>  show tables;

What's the output of this command?

mysql>  desc products;

What's the output of this command?

Thank you,

-Conrad

Hi Taylor and Brian,
I have already taken a second look at my code, thanks. The book is what
it is and Julian seems to agree.
There is nothing on interpreting the error messages in the browser or
the logs in the book up to the point they begin appearing.

In any case, the tables and columns are as follows:

mysql> show tables;
±----------------------------+
| Tables_in_depot_development |
±----------------------------+
| products |
| schema_info |
±----------------------------+
2 rows in set (0.00 sec)

mysql> desc products;
±------------±--------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±------------±--------------±-----±----±--------±---------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(255) | YES | | NULL | |
| description | text | YES | | NULL | |
| img_url | varchar(255) | YES | | NULL | |
| price | decimal(10,0) | YES | | 0 | |
±------------±--------------±-----±----±--------±---------------+
5 rows in set (0.48 sec)

mysql>

rake db:migrate comes out ok

the new product button returns this:

Showing app/views/admin/_form.rhtml where line #11 raised:

undefined method `image_url’ for #Product:0x3335a9c

Extracted source (around line #11):

8: <%= text_area ‘product’, ‘description’ %>


9:
10:

Image url

11: <%= text_field ‘product’, ‘image_url’ %>


12:
13:

Price

14: <%= text_field ‘product’, ‘price’ %>

Trace of template inclusion: /app/views/admin/new.rhtml

RAILS_ROOT: script/…/config/…

not much changed. any idea? maybe something really obvious like im
running the wrong version of something?

Hi, you need to rename the field in your database:
img_url

to

image_url

Now, do the following in the root directory of your rails project (i.e.
depot) within the command prompt or Unix shell.

rake db:migrate VERSION=0

cat 001_create_products.rb

-Conrad

Now, do the following at the command prompt:

rake db:migrate

Next, restart webrick or mongrel. Try it out and let me know.

-Conrad

Sent from my iPhone

On Jul 31, 2007, at 2:55 PM, Cameron Cameron
<[email protected]

like this?

mysql> alter table products change img_url image_url varchar(255) null;
Query OK, 0 rows affected (1.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc products
-> ;
±------------±--------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±------------±--------------±-----±----±--------±---------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(255) | YES | | NULL | |
| description | text | YES | | NULL | |
| image_url | varchar(255) | YES | | NULL | |
| price | decimal(10,0) | YES | | 0 | |
±------------±--------------±-----±----±--------±---------------+
5 rows in set (0.05 sec)

mysql> exit
Bye
camerons-powerbook-g4-15:~/sites/depot Cameron$ rake db:migrate
VERSION=0
(in /Users/Cameron/Sites/depot)
== AddTestData: reverting

== AddTestData: reverted (0.0517s)

== AddPrice: reverting

– remove_column(:products, :price)
-> 0.1747s
== AddPrice: reverted (0.1765s)

== CreateProducts: reverting

– drop_table(:products)
-> 0.0960s
== CreateProducts: reverted (0.0989s)

camerons-powerbook-g4-15:~/sites/depot Cameron$ cat
001_create_products.rb
cat: 001_create_products.rb: No such file or directory
camerons-powerbook-g4-15:~/sites/depot Cameron$ cd db/migrate
camerons-powerbook-g4-15:~/sites/depot/db/migrate Cameron$ cat
001_create_products.rb
class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.column :title, :string
t.column :description, :text
t.column :image_url, :string
end
end

def self.down
drop_table :products
end
endcamerons-powerbook-g4-15:~/sites/depot/db/migrate Cameron$ cd …
camerons-powerbook-g4-15:~/sites/depot/db Cameron$ cd …
camerons-powerbook-g4-15:~/sites/depot Cameron$

ok, where am i at now?
thanks again
-Cameron

Conrad T. wrote:

Hi, you need to rename the field in your database:
img_url

to

image_url

Now, do the following in the root directory of your rails project (i.e.
depot) within the command prompt or Unix shell.

rake db:migrate VERSION=0

cat 001_create_products.rb

-Conrad

Cameron:

I have already taken a second look at my code, thanks.

Sorry, didn’t mean to sound condescending, We’re trying to help you,
please
keep that in mind. I wanted to assure you that there’s nothing wrong
with
the book. The problem you’re having is in your code, or more
specifically,
in your database.

As you already are probably aware:

img_url does not equal image_url.

Rails auto-generates methods in models based on your table definitions.
Since you had no column in your database table called ‘image_url’, you
were
getting that NoMethodError error.

For future reference, you should avoid using ALTER TABLE / CREATE TABLE
statements if you are using Migrations. You can get into trouble there.
Migrations issue all of the alter / create statements for you. You
really
need to choose between the two methods (migration vs managing your own
schema). They don’t mix well :slight_smile:

I hope everything works out for you. Remember, we’re a helpful bunch, so
keep the questions coming.

Brian H. wrote:

Cameron:

I have already taken a second look at my code, thanks.

Sorry, didn’t mean to sound condescending, We’re trying to help you,
please
keep that in mind. I wanted to assure you that there’s nothing wrong
with
the book. The problem you’re having is in your code, or more
specifically,
in your database.

As you already are probably aware:

img_url does not equal image_url.

Rails auto-generates methods in models based on your table definitions.
Since you had no column in your database table called ‘image_url’, you
were
getting that NoMethodError error.

For future reference, you should avoid using ALTER TABLE / CREATE TABLE
statements if you are using Migrations. You can get into trouble there.
Migrations issue all of the alter / create statements for you. You
really
need to choose between the two methods (migration vs managing your own
schema). They don’t mix well :slight_smile:

I hope everything works out for you. Remember, we’re a helpful bunch, so
keep the questions coming.


Hi Brian and Conrad,

Your help is much appreciated. The book came highly recommended so I
need to persevere. Perhaps you can see my error. There is no doubt I’m
learning more by dealing with these errors :). Best, Julian

I’ve followed the suggestions above:

1)checked the image_url field
2)Rolled back the migration
3) Called the Rake db:migrate again
4) Still getting
rake abort
undefined method `image_URL’ for #Product:0x3d98368

and got the following results:

mysql> use depot_development
Database changed
mysql> show tables;
±----------------------------+
| Tables_in_depot_development |
±----------------------------+
| products |
| schema_info |
±----------------------------+
2 rows in set (0.00 sec)

mysql> desc products;
±------------±-------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±------------±-------------±-----±----±--------±---------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(255) | YES | | NULL | |
| description | text | YES | | NULL | |
| image_url | varchar(255) | YES | | NULL | |
| price | decimal(8,2) | YES | | 0.00 | |
±------------±-------------±-----±----±--------±----------

-database field image_url is OK

C:>cd work

C:\work> cd depot

C:\work\depot> rake db:migrate VERSION=0
(in C:/work/depot)
== AddPrice: reverting

– remove_column(:products, :price)
-> 0.5000s
== AddPrice: reverted (0.5000s)

== CreateProducts: reverting

– drop_table(:products)
-> 0.0620s
== CreateProducts: reverted (0.0620s)

C:\work\depot> cat 001_create_products.rb
cat: 001_create_products.rb: No such file or directory

C:\work\depot> rake db:migrate
(in C:/work/depot)
== CreateProducts: migrating

– create_table(:products)
-> 0.1880s
== CreateProducts: migrated (0.1880s)

== AddPrice: migrating

– add_column(:products, :price, :decimal, {:precision=>8, :scale=>2,
:default=>
0})
-> 0.2660s
== AddPrice: migrated (0.2660s)

== AddTestData: migrating

rake aborted!
undefined method `image_URL’ for #Product:0x3d98368

(See full trace by running task with --trace)

C:\work\depot>

class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.column :title, :string
t.column :description, :text
t.column :image_url, :string
end
end

def self.down
drop_table :products
end
end

Jacob A. wrote:

Julian Bourne wrote:

1)checked the image_url field
2)Rolled back the migration
3) Called the Rake db:migrate again
4) Still getting
rake abort
undefined method `image_URL’ for #Product:0x3d98368

Why are you using image_URL with capitalized url? Try it with lowercase.


Cheers,

  • Jacob A.

Hi Jacob,

MEA CULPA or should I say mea culpa. Thank you for taking the time to
check my code.

Cheers,
Julian

Julian Bourne wrote:

1)checked the image_url field
2)Rolled back the migration
3) Called the Rake db:migrate again
4) Still getting
rake abort
undefined method `image_URL’ for #Product:0x3d98368

Why are you using image_URL with capitalized url? Try it with lowercase.


Cheers,

  • Jacob A.