Help for Rails Beginners using the book AWDwR and Rails 2.0
December 18th, 2007 | by WoonZai |This post is for beginners for Ruby on Rails who is learning from the Depot tutorial from the book: Agile Web Development with Rails 2nd Edition. The release of Rails 2.0.2 had deprecated some functions, and changed some defaults. I’m only started with Ruby on Rails for less than a week and still in the process of clearing a lot of doubts out.
This entry will mainly focus on getting you up and ready for Chapter 6 >> Iteration A1: Get Something Running. Specifically the parts I’ve identified so far that will cause problem is the switch in default database to sqlite3 (There should not be any problem if you have used sqlite3 for your database, but the ebook uses mysql for the walk-through)
This should cause the following error message in your command when you try to run rake:migrate
"require 'sqlite3'" gives "no such file to load -- sqlite3 (LoadError)"
To solve this problem, instead of running the following command in Chapter 6.1 (Pg 68)
rails depot
Run the following command to use mySQL as the default database.
rails -d mysql depot
There’s also a shortcut you can use. You can skip the following code to create a development database in mySQL manual on Page 69.
mysqladmin -u root create depot_development
Instead, you can go ahead to test your configuration as indicated on Page 71 through the following code
depot> rake db:migrate
If you don’t experience any error with the above command, it means that Rails is able to connect to your mySQL database server.
You might ask, “But I have not created my depot_development database yet”! That is where the beauty of Rails actually lie. Instead of going into the command prompt to create the database, you can use the rake command below to create the database for you.
rake db:create:all
The command creates three mySQL database for you, namely depot_development, depot_production, and depot_test. For the purpose of the tutorial, we just need the depot_development database, so you can type the following command
rake db:create
This automatically creates the development database for your current application.
The second change you’ll probably come across is due to a change in how scaffolding is performed in Rails 2.0.2. You will probably encounter the following error message when you try to access the URL (http://localhost:3000/admin).
As of now, I still can’t figure out how to draw the link between the Product model and the admin controller in the tutorial to work together yet. However, instead of following the instructions from Page 74 - 76 by running ruby script/generate model product, you can run
ruby script/generate scaffold Product title:text description:text image_url:string
A file named 001_create_products.rb will be created, with all the columns included, plus a default t.timestamp value. The t.timestamp automatically adds 2 columns of type datetime to your database (created_at, and updated_at).
You can access the same page in Pg 77 of the book, by going to http://localhost:3000/products. The new Rails scaffold automatically creates a controller named
script/generate scaffold ModelName [ControllerName] [action, ...]
I’m still trying to figure out why my the scaffold does not recognize my command
ruby script/generate scaffold Product Admin title:text description:text image_url:string
I get the following command line error
wrong number of arguments (1 for 2)
I update this post once I find the answer. Or if you happen to have solved that problem, let me know! ![]()
3 Responses to “Help for Rails Beginners using the book AWDwR and Rails 2.0”
By Chu Yeow on Dec 29, 2007 | Reply
Ah, the scaffold generator has changed from AWDwR - the syntax is now “Usage: script/generate scaffold ModelName [field:type, field:type]”
You can simply run script/generate scaffold for the latest syntax and usage examples.
By Joe Lewis on Jan 4, 2008 | Reply
As another new Rails developer hitting the AWDWR book with a fresh 2.0 install, this was very helpful. This has been a rough start.
I believe that jumping in to the 2.0 game is important for the future, but I find it unfortunate and frustrating that so much key stuff right off the bat was broken between it and this key book.
One tip for the newbies if it isn’t obvious: Don’t forget to run rake db:migrate after generating the scaffold. If you see an error that reads:
ActiveRecord::StatementInvalid in ProductsController#index
Mysql::Error: Table ‘depot_development.products’ doesn’t exist: SELECT * FROM `products`
then you still need to run rake db:migrate.
By bg on Jan 7, 2008 | Reply
Thanx for this article and especially for
rails -d mysql xxx
I spent 2 days trying make hello-world-application works (on Agile tutorial)… Now it works