Ruby on Rails – RoR

Recently I’ve just taken a quick look into Ruby on Rails (RoR) and all I can say is – WOW (followed by why don’t “other” languages we know support some of these features straight out of the box :-P). Like I said I’ve only just started looking at it after /\ndrew (start blogging!) gave both JD and myself a quick tutorial/run down on how to get started. The things I love so far are: how quickly you can get started, the power of the framework, how easy it is to get things setup and get working (I normally get annoyed with some of the new/newish tools out there because you have to hack at them to get them work – the RoR stuff is far from that – it really does just work!), the Active Record pattern, the consistancy of the solution setup, the out of the box test framework, … the list goes on.

There really is a lot of cool stuff in it and you can get running really, really quickly. Rather than write about all the cool stuff (because theres just so much) my best advice would be to go and grab the Instant Rails (IR) package at http://instantrails.rubyforge.org/wiki/wiki.pl , install it (gives you RoR, Apache and MySQL), and then go through the 2 tutorials at http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html . These services can all be started and stopped with 1 click which makes the IR package really cool – No editing config files here! 😀 Since /\ndrew gave us a bit of a run down first the tutorial was really a breeze. Here’s my dump of things I came across/notes that might not be immediatelly obvious:

1) Install the IR package to the c drive. Although it does an auto configuration if you install it somewhere else I found the scaffolding command (see the tute) when run from the command line didnt generate all the files needed. Maybe it was just me – maybe not….seemed to work once it was on the c drive anyway…

2) Edit the IR “use_ruby.cmd” file as per the online instructions. Then change your Apache web server to run off port 3000 (right click on the IR taskbar icon). This is the same port as the same as the WeBrick server that you use when working with your Ruby Apps. This will stop it complaining that there is already IIS running on port 80. The other option is to stop IIS…

3) Copy the iconv.dll from ../ruby/lib to ../ruby/bin. It says this on the IR setup page – I got the problem they talk about though so it must just be a step to make sure you do.

4) In addtion to the IR package you will need a database viewer so you can look at your mysql database. The tute suggests one but if you are new to this then you could also grab the MySQL one from here (free – not a trial): http://dev.mysql.com/downloads/query-browser/1.1.html . When you run this up just connect to the running database (start IR first) with localhost, user = sa, password left blank, port = 3306. You might want to change the password but just get things working first…

5) Play with some RoR :-). To help out here use an editor like SciTE (has an inbuilt propmt which you can use to run your ruby commands from – only problem is the delete key is not forwarded to the command prompt so you have to type it right the first time :-P) and also grab the radrails ide: http://www.radrails.org/

So how do I get started with my play apps: (this is also for me so I dont forget – I’m learning too here :-P):

Open a command prompt and run the cmd file “use_ruby.cmd”, run the command “rails ProjectNameHere” – this creates a whole lot of files under /ruby_apps/ProjectNameHere, now open radrails, pick new Rails project, untick the options (for the project skeleton and the WeBrick server), point to the directory under ruby_apps that was just created. Cool – now browse to the config directory and edit the database.yml file (see the online tute). We’re away 🙂 You can now use the database viewer inside the radrails ide – if you are experimenting with passwords etc then this will tell you if you can connect or not – if you can see your files then your config file is all good 🙂

The things that may trick you at first are: RoR follows a naming convention so if you name things in a certain way you can get a lot of wireup for free. Some of these are: name the primary key of a database table “id”. Name tables by there group name e.g. companies, employees etc rather than company, employee… Name foriegn key columns by singularName_id. So an employee would be linked to a company with a column named “company_id”. It’s actually really clever here (I’m not sure if this will bite me one day) but it knows that the singular form of companies is company and not “companie”. So when you run your scaffolds etc use the singular name – otherwise things won’t work auto-magically :-).

Once you have a table you can run various scripts but the fastest is to run a command in the format “rails script\generate scaffold Company”. Which would create all the things necassary for us to start working against the Companies table. This is quite good because it will also write some stub tests as well. You can run all of these tests, check your sripts, commands etc by running the command “rake” (you may need to browse inside the project directory from your command prompt so rake can see the rake file). And check your code by running “rake stats”. After this you pretty much have a simple site – easy a…..(use the command “ruby script\server” to run it up – do this in another command prompt). Once again have a look at the tutes for the full story…. An interesting thing to note is code layout – use good design principles (MVP/MVC) etc to make your code understandable. The tutorial isnt in this format but obviously its just so you can get started…..something to keep in mind though (check the comments for agreement on this one) 🙂

After that go here: http://api.rubyonrails.org/ and experiment with whats available – all I can say is its fast development wise and very cool. Finally, run rake to find out if it knows about a command, and restart the server if something isn’t refreshing 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: