Blog
Sunday, February 27, 2005
Ruby on Rails
After Kristian having mentioned Ruby on Rails (RoR) numerous times, I finally decided to give it a go. From what Kristian mentioned and from what I have read on the net, RoR should be able to make web developers much more efficient.
Installation:
In order to install RoR you first need to install Ruby - obviously.
# urpmi ruby
RoR is primarily distributed using the RubyGems tool, from what I can tell it works basically like Perl's CPAN, apt-get, urpmi etc. First you fetch the RubyGems package from RubyForge.
# tar zxf rubygems-0.8.4.tgz
# cd rubygems-0.8.4
# ruby setup.rb
Before running the post-installation script, I had to comment out the line that attempts to install windows batch files, since that procedure does not work on my Linux system.
# ruby post-installed.rb
# gem install rails
Accept all the dependencies, and you should have a fully functional RoR installation.
Testing the installation
Next of, I decided to create a small application in order to test the RoR. I a few weeks I am going on a trip to San Francisco, and we are currently planning the trip, so why not create an application that can help.
The RoR development strategy is based on:
DB design -> Controllers -> Model -> Layout
or at least that is how I remember reading it :-)
The clever thing about RoR is that based on your database design it will automatically map entries in the database to instances of objects. I wanted to manage a set of events, thus I created a controller named event and a database table named events (RoR will look for a plural version of your controller, and is smart enough to look for "companies" when you have a controller named "company").
I had read that RoR would make it possible for me to specify many-to-one, one-to-many relationships between two objects, and wanted to see how easy it would be to implement and how useful it would be.
My Event class got a
belongs_to :day
And my Day class got
has_many :events
Having at the same time added a day_id to the events database table, all day objects would now have a .events array of all events within that day. I then do not have to handle all data retrieval my self.
Scaffolding
Scaffolding make it easy to start working with data manipulation (create, update, view and list) and automatically handles entries in each data element based on the database schema. Just insert the following line in a controller class will create the framework for manipulating that data object automatically:
scaffold :day
Next, I wanted to change the default look of the day list, so I could get a full view of our trip. I added a definition for the list action, and changed the behavior so that it
When users go http://host/day/list the will get the new presentation of the trip plan.
The Scaffolding feature is not intended to be used in the final application, but is meant as a way to make it easy to manipulate data in the start-up phase.
Specifying layout
After having specified layout of the planning overview, changed creation and update formulas for events it was time to create a layout for the application. I created a standard.rhtml file in app/views/layouts/standard.rhtml.
Adding the following line to both my controllers (day and event) make both controllers use the layout.
layout "standard"
Cleaning up behavior
I added a few buttons on the overview page, so users could go directly to the "Add new event" page. But when users created or updated an event, they would return to event/list or event/show/123. Instead I wanted them to return to the overview page, easy enough, just had to add a small redirect in each of the actions
redirect_to(:controller => "day", :action => "list")
Roundup
That is it, I have finished my small trip planner web application within a few hours. My first impressions are that Ruby on Rails is a neat framework that in some cases could actually speed-up application production. Some of the helpers are really cool, and the whole idea of working with controllers for web applications is neat. Actually we are already doing this in large parts of Fundanemt :-) Ruby on Rails does as well try as hard as it can to make URLs as readable as possible (that was a lot of "as" haha), and as a big fan of readable URLs that just makes me wanna go WOHOO.
One of the down sides of Ruby on Rails is that it is based on Ruby, thus developers will have to learn a new programming language (how ever beneficial this new language might be) I do not find the syntax all that intuitive.
Anyway, that is it for now, I might return with more to report when I have dug deeper into Ruby on Rails. Potential NEAT is all I can say at the moment :-)
Installation:
In order to install RoR you first need to install Ruby - obviously.
# urpmi ruby
RoR is primarily distributed using the RubyGems tool, from what I can tell it works basically like Perl's CPAN, apt-get, urpmi etc. First you fetch the RubyGems package from RubyForge.
# tar zxf rubygems-0.8.4.tgz
# cd rubygems-0.8.4
# ruby setup.rb
Before running the post-installation script, I had to comment out the line that attempts to install windows batch files, since that procedure does not work on my Linux system.
# ruby post-installed.rb
# gem install rails
Accept all the dependencies, and you should have a fully functional RoR installation.
Testing the installation
Next of, I decided to create a small application in order to test the RoR. I a few weeks I am going on a trip to San Francisco, and we are currently planning the trip, so why not create an application that can help.
The RoR development strategy is based on:
DB design -> Controllers -> Model -> Layout
or at least that is how I remember reading it :-)
The clever thing about RoR is that based on your database design it will automatically map entries in the database to instances of objects. I wanted to manage a set of events, thus I created a controller named event and a database table named events (RoR will look for a plural version of your controller, and is smart enough to look for "companies" when you have a controller named "company").
I had read that RoR would make it possible for me to specify many-to-one, one-to-many relationships between two objects, and wanted to see how easy it would be to implement and how useful it would be.
My Event class got a
belongs_to :day
And my Day class got
has_many :events
Having at the same time added a day_id to the events database table, all day objects would now have a .events array of all events within that day. I then do not have to handle all data retrieval my self.
Scaffolding
Scaffolding make it easy to start working with data manipulation (create, update, view and list) and automatically handles entries in each data element based on the database schema. Just insert the following line in a controller class will create the framework for manipulating that data object automatically:
scaffold :day
Next, I wanted to change the default look of the day list, so I could get a full view of our trip. I added a definition for the list action, and changed the behavior so that it
- Get a list of all days
- Sort the list based on a timestamp
- Let the layout template for the list handle the data presentation.
- Create a file app/views/day/list.rhtml
- Specify presentation in the list.rhtml file.
When users go http://host/day/list the will get the new presentation of the trip plan.
The Scaffolding feature is not intended to be used in the final application, but is meant as a way to make it easy to manipulate data in the start-up phase.
Specifying layout
After having specified layout of the planning overview, changed creation and update formulas for events it was time to create a layout for the application. I created a standard.rhtml file in app/views/layouts/standard.rhtml.
Adding the following line to both my controllers (day and event) make both controllers use the layout.
layout "standard"
Cleaning up behavior
I added a few buttons on the overview page, so users could go directly to the "Add new event" page. But when users created or updated an event, they would return to event/list or event/show/123. Instead I wanted them to return to the overview page, easy enough, just had to add a small redirect in each of the actions
redirect_to(:controller => "day", :action => "list")
Roundup
That is it, I have finished my small trip planner web application within a few hours. My first impressions are that Ruby on Rails is a neat framework that in some cases could actually speed-up application production. Some of the helpers are really cool, and the whole idea of working with controllers for web applications is neat. Actually we are already doing this in large parts of Fundanemt :-) Ruby on Rails does as well try as hard as it can to make URLs as readable as possible (that was a lot of "as" haha), and as a big fan of readable URLs that just makes me wanna go WOHOO.
One of the down sides of Ruby on Rails is that it is based on Ruby, thus developers will have to learn a new programming language (how ever beneficial this new language might be) I do not find the syntax all that intuitive.
Anyway, that is it for now, I might return with more to report when I have dug deeper into Ruby on Rails. Potential NEAT is all I can say at the moment :-)
posted by Brian Jørgensen at 7:58pm.
permanent url: http://www.qte.dk/blog/archive/20
permanent url: http://www.qte.dk/blog/archive/20
Rss feed