SpecialsHome > Specials > ColumnsPimp My Rails: The Rails Framework
walski
on 04/05/2008 07.29 PM
The pimping continues: In the last episode I've shown you how to setup a Rails environment on your local computer and we've created a first Rails application. Today we're getting into details of the Rails framework and it's principles.
Welcome back! I promised to show you how easily a authentication system can be integrated in your Rails application, but we won't do that this week. My fault! But the article got to long and so we've decided on shifting this explanation to the next week's article. But now let's rock on! First of all I would like to tell all the Windows fanboys out there about an interesting discussion, going on at the moment in the Ruby world, about the support of Windows as a platform for running Ruby. You find an interesting write-up about this here.
Before we're going into the details of user authentication, it's important that you understand how a Rails application basically works. Rails follows three main principles:
All of these principles intersect at some point.
MVC - Model View Controller This concept was first described by Trygve Reenskaug, 1979. Somewhen in the 1990s someone had the brilliant idea, that reinventing the wheel is the worst of all options and thought that what was right for the GUI development since more than 10 years, couldn't be so wrong for the relatively new web development. This was a wise guy. Unfortunately I don't know whom to thank for this great idea of returning to what was true before, but, take it for granted that MVC is terrific! :) But what is it? Generally it means that your code-base is split up into three parts:
DRY - Don't repeat yourself This one is simple: NEVER EVER COPY CODE! No rule without exceptions, but for 99% of your code take this advice from many really experienced Rails developers: If you come to a point in your code, where you would like to copy&paste from another method or class or whatever, go the extra mile, outsource this code, so that both places can call ONE method. Rails offers you good tools ease the pain of this extra mile and you'll be soooo much happier when you have to refactor and maintain your code. Actually this rule will help you in any programming language, you will ever learn, but the Rails community hypes it more, than any other language, I know and this is good! CoC - Convention over Configuration Unlike the DRY principle, CoC was somehow a new experience for many developers when they got to Rails. In the meantime many other popular web frameworks (like Django for Python and others) have adopted it or developed similar solutions independently of Rails, but it turns out to be a very good idea: Instead of confronting the developer with the necessity to configure every little aspect of the framework, Rails just asks you for your database host, user and password and anything else is ready, out-of-the-box! Don't get this wrong: You CAN configure everything, but in my experience you won't even touch 90% of all the options even for larger projects. But Rails goes one step further with so called generators. These a little scripts which supports you at the creation and deletion of any kind of thing in Rails. Instead of just starting with a new file and then building a controller, model or view from that file, generators allow you to generate e.g. controllers or models. Such generated files already have the basic skeleton of a controller or model and you don't have to mess with the boring creation of infrastructure, the generators does this for you. And it goes even further with the creation of so called scaffolds which in fact are complete bundles of a model, a view and a controller which offer you the basic CRUD (Create/Read/Update/Delete) operations on the model. This should be enough theory for now. Let's get back to actual code!
Now we're going to create our first controller. If you like, you can rename the "test_application" directory, which the rails command created for you during last weeks tutorial, to whatever you like. Then fire up your terminal, change into that folder and do this: script/generate controller startpage Cool, you see a list of files which the generator has created for you. The interesting file is the startpage_controller.rb in the app/controllers folder of your application. Open up this file.
Now you should see something like that:
class StartpageController < ApplicationController To display our first page we'll now add an so called action and after that the corresponding view. Edit the file like this:
class StartpageController < ApplicationController Technically the block between def and end is a method like in any other programming language. The name of the function is index. For Rails any method in a controller class (a class which derived from ApplicationController class) (the < sign after the class name is the same as the extends keyword in language like PHP and Java) is an action. An action can be called by an URL. The default behavior of Rails is, that any URL is built like this:
http://yourhost:serverport/controller_name/action_name/index_variable The controller_name is startpage in our case, the action_name is index and the index_variable is empty. This index_variable can be used to pass an argument to the action by the URL.
This: http://localhost:3000/startpage/index/5 So the index_variable is just some eye-candy for the URL and it's accessible under the name index inside the actions. You can easily define more of such variables which are encoded in the URL and have different names than index, as well. But more on that routing topic later on.
Before we can try our controller we have to create a view for the index
action! Go to the app/view/startpage folder of you app an create a new
textfile in your editor named "index.html.erb". The file-extension of Rails
2.0 is "markup-language.renderer". In our case we would like to create
a html file and the renderer we use is the default Rails renderer, named
erb (embedded Ruby). You can imagine other file-extensions like ".rss.erb"
of ".xml.erb". Don't worry about the renderer. Changing that is an advanced
topic which isn't complicated, but isn't really needed at the beginning,
too. In the created "index.html.erb" you will write the HTML, that your controller should deliver to the browser. So let's start with a no-brainer: Hi, I'm your brand new Rails application! Save that. Now you need to start your server up and again, as described in part I of this tutorial. Just jump into the root folder of your application and enter: script/server Then call the newly created action in your browser, as described above: http://localhost:3000/startpage/index if you like you can also call this just by http://localhost:3000/startpage because index is the default action which Rails is choosing if no action is specified.
You should be able to see a friendly, white page with the "Hi..." text,
you've entered above. Now lets spice it up. There are to techniques to insert Ruby code into your view:
So the PHP equivalents to that are:
Most of the time you will need the <% ... %> tag for if-clauses or
something like that and the <%= ... %> tag will be used to insert something
into your templates. Hi, I'm your brand new Rails application! <% if @test_variable.size >= 5 %> YES! The test_variable is long enough! Here it is: <%= @test_variable %> <% end %> Now you've to take a look at the code in the startpage_controller.rb file, we've edited above. There is this line: @test_variable = "Hello" This defines an instance variable named test_variable which holds the "Hello" string. Look at this PHP example
class SimpleClass { The $var variable in this example is an instance variable. It is available to the instance of an object. So our @test_variable variable is valid throughout the while life-cycle of an StartpageController instance. If we would have written: test_variable = "Hallo"
(Pay attention to the missing @) the test_variable variable would only
be available in the "index" method.
Web development with Ruby on Rails:
|
Comments
| |||
|
|
Games Convention: Bigpoint, Gameforge, upjers, Mediatainment... |
|
|
Thinking about why |
|
|
Rails Pimpin' Continues |
|
|
Framework for PBBGs? - DB Planning |
| 1 | (1) |
|
Khan Wars |
| 2 | (2) |
|
Bulfleet |
| 3 | (3) |
|
Tycoon Online |
| 4 | (4) |
|
Mobstar |
| 5 | (5) |
|
IceWars |
| 6 | (6) |
|
Mythlands |
| 7 | (7) |
|
Crimson Moon |
| 8 | (8) |
|
ZeldereX Online |
| 9 | (9) |
|
Holy-War |
| 10 | (10) |
|
World of Dungeons |
|
|
Cromm Linked: MMOs vs the World |
|
|
Dreighton Marking a milestone |
|
|
RangerSheck Statistics: What are the real odds? |
|
|
PrivateSniper Created Group |
|
|
Leinad WORLD WARS |



