Thursday, 25 September 2008
Learning Rails - Part 2
I wanted to make this a series about Rails and about Ruby. To that end I'm going to write a series of entries on building an MVC application. As I said in the previous post I am a Ruby and Rails neophyte, so I'll be learning as I go along, regard this as a developer's journal. The aim being that you can learn from the mistakes that I make. If this really gets going I also hope to do some screencasts along the way.
What am I going to write - a blog of course (is there any other sort of web app out there today :) ). I chose blogging software because
- Everybody understands what a blog is, so no trying to figure out the behaviour
- It gives me a chance to play with different formats (html and RSS)
- At some point I'll need to write support for the various APIs (atom, etc)
- There'll be chances to use AJAX
- I can learn how to deploy to Apache
- It shouldn't take too long to get things going
I'm going to rely heavily on the 'Agile Development With Rails' book, and hopefully any feedback I get. I'm learning Ruby at the same time as Rails and Ruby is a very idiomatic language, I know I'll get some of the idioms wrong, so again feedback is welcome. Oh and initially I'm doing this on MacBook using TextMate as the editor. At some point I'm going to play with NetBeans and Eclipse as tools and so try out JRuby, not that I like a challenge of learning 17 new things at once!
Start at the beginning. Go on the end and then stop, said the Red Queen
So the beginning of a Rails app is the code generator, to create the application I fire up a terminal make sure I'm in the right directory and call
rails rblog
This gives the well known directory structure of
app config db doc lib log public script test tmp vendor
Most of the time I'll be in the app directory (that's where the models, controllers and views are), but I'll also use stuff from the db directory where the 'migrations' are stored, in the public directory where public the web files are and in config, where various setup files live.
Once the application has been created I can fire up another terminal (I usually have three open, one for my commands, one for the server and another to see the log file), change the the project directory and run the server with the command 'script/server'
This starts the server on port 3000, the server I'm using here is Mongrel, you can also start WEBrick. There's plenty of discussion on these servers out on the web so I won't go into the differences here.
Once the server is running you can point your browser at http://localhost:3000 and you should see something like:

I'm going to use MySql for the database and this needs to be configured. To do this I've edited config/database.yml to look like this:
development: adapter: mysql encoding: utf8 database: rblog_development username: root password: socket: /tmp/mysql.sock
Adding similar entries for test and production databases. Once that's done I created the development database
mysqladmin -u root create rblog_development
Now that all the necessary structure is in place I can create the first controllers and models. What do I know about the blog? I know it's going to have users, users can have blogs and blogs will have entries and comments. There may be more things eventually, such as tags, categories, pingbacks etc, but for now that's enough. So not to get too far ahead of myself I decided to create scaffolding for users, blogs and blogentries.
For the users model I'm going to user a similar approach to that used in the Rails book, so the user will have an email address and password, the password will be stored as a hash will be salted. A user will have a name that can be displayed on comments or on a blog and users may also own blogs. For now I'm going to limit this to one blog per user, but in the future this may expand to multiple blogs.
Running
script/generate scaffold user email:string hashed_password:string name:string blog_id:integer
gives me what I want. Doing something similar for blogs and blogentries
script/generate scaffold blog title:string sub_title:string owner_id:integer admin:boolean script/generate scaffold blog_entry title:string entry:text author_id:integer entry_added:datetime entry_last_edited:datetime
creates those models and their corresponding controllers and migrations.
Notice that as in my previous post I'm using singular names for the model components, so user, blog and blog_entry.
Now that's done it's then onto setting out some of the UI and writing some unit testing code.
Posted by at 7:32 AM in Ruby
[Trackback URL for this entry]
