Wednesday, July 12, 2006

Configuring the Application

Rails uses a flat file located in config/database.yml to store the information on how to connect to the database. This file should be edited to look like this:
# MySQL (default setup).  Versions 4.1 and 5.0 are recommended.
#
# Install the MySQL driver:
# gem install mysql
# On MacOS X:
# gem install mysql -- --include=/usr/local/lib
# On Windows:
# There is no gem for Windows. Install mysql.so from RubyForApache.
# http://rubyforge.org/projects/rubyforapache
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
adapter: mysql
database: student
username:
password:
host: localhost

# Warning: The database defined as 'test' will be erased and
# re-generated from your development database when you run 'rake'.
# Do not set this db to the same as development or production.
test:
adapter: mysql
database: student_test
username: root
password:
host: localhost

production:
adapter: mysql
database: student_production
username: root
password:
host: localhost

Now let us write the maintenance application. In a command window type:
c:/rails/work/student>ruby script/generate scaffold Participant Admin

That single command has written a basic maintenance application. The Participant parameter told the command the name of the model we want, and the Admin parameter specifies the name of the controller. Let's try our application. First, we'll start a local WEBrick-based web server, supplied with Rails. Let's connect to it. Remember, the URL we give to our browser contains both the port number (3000) and the name of the controller in lowercase (admin) ie. http://localhost:3000/admin

It's showing us a list of participants, and there aren't any participants. Let's remedy that. Click the New participant link, and a form should appear. Fill in the details and click on the Create button, you should see the new participant in the list. See the screenshot. The User Interface is not good, but it works. We can show this to our client for his/her approval. The client can play with the other links (showing details, editing existing participants etc.).

Rails Scaffolds
A Rails scaffold is an autogenerated framework for manipulating a model. When we run the generator, we tell it that we want a scaffold for a particular model (which it creates) and that we want to access it through a given controller (which it also creates). In Rails, a model is automatically mapped to a database table whose name is the plural form of the model's class. In our case, we asked for a model called Participant, so Rails associated it with the table called participants. And how did it find that table? We told it where to look when we set up the development entry in config/database.yml. When we started the application, the model examined the table in the database, worked out what columns it had, and created mappings between the database data and Ruby objects. We created the participant maintenance scaffolding in the Admin controller, which is why the URL that accesses it has admin at the start of its path. The scaffold is the starting point of an application, not a finished application. And we're about to make use of that fact as we move on to the next iteration in our project.

First Post | Previous | Next



Technorati Tags:
Blogs linking to this article

7 Comments:

Blogger Ashish Kulkarni said...

Satish,

Can I use PostgreSQL by simply changing the name of the adapter?

What additional steps will I have to take?

Again, same question for Oracle.

Regards,

Ashish.

1:53 PM  
Blogger Unknown said...

There is a pure-Ruby version of the Postgres adapter available. Download
postgres-pr from the Ruby-DBI page at http://rubyforge.org/projects/ruby-dbi

The available database adapters are available here -
Oracle http://rubyforge.org/projects/ruby-oci8
Postgres http://ruby.scripting.ca/postgres/

3:09 PM  
Blogger Unknown said...

Raj, did you modify the file config/database.yml where

development:
adapter: mysql
database: student
username:
password:
host: localhost

3:36 PM  
Blogger Aspirations said...

exists app/controllers/
exists app/helpers/
exists app/views/admin
exists test/functional/
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
identical app/models/participant.rb
identical test/unit/participant_test.rb
identical test/fixtures/participants.yml
error Before updating scaffolding from new DB schema, try creating a table for your model (Participant)

Not sure why the error in bold is coming. Also, when trying to access http://localhost:3000/admin it says "Routing Error: Recognition failed for /admin"

4:02 PM  
Blogger Unknown said...

Anish, confirm that your table participants exists in the database student

4:05 PM  
Blogger Ashish Kulkarni said...

Finally got some time this weekend to do this...

And I have decidided to go with the dark side - MySQL.

After all, this is just an acedemic exercise. So don't need a commercial license.

The result:

Had to do a few tweaks with the database.yml - Needed to put root's password in due to my security configuration. However, in the end, I must admit, I am impressed.

The only 2 applications that allow data entry right after table creation like this are FoxPro (my first love) and MS-Access (the one that I use even today at times).

6:03 PM  
Anonymous Anonymous said...

Satish,

while running the script
ruby script/generate scaffold Participants Admin

following is the output....

exists app/controllers/
exists app/helpers/
exists app/views/admin
exists test/functional/
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
identical app/models/participant.rb
identical test/unit/participant_test.rb
identical test/fixtures/participants.yml
No such file or directory - /tmp/mysql.sock

then in the browser following is the error

Routing Error

Recognition failed for "/admin"

Regds,
Arun

2:37 AM  

Post a Comment

<< Home