Hello World from View
October 5, 2009
1) First step is choosing folder on file system for keeping rails applications together. For example I will use folder named ‘rails_apps’ in root of c drive. To accomplish this open command prompt window and type next four lines:
c:
cd \
md rails_apps
cd rails_apps
2) Now when we are inside ‘rails_apps’ folder, we will create structure of rails application by executing:
rails helloworld
3) Previous step created for us new folder named ‘helloworld’ inside ‘rails_apps’, we will have to navigate inside it before executing next steps:
cd helloworld
4) Now we are ready for creating our own code. First thing that we will create is controller class. Name of controller will be ‘say’, to do this type next line in command prompt:
ruby script/generate controller say
5) After previous step there are new folders and files generated for us. We will first look for file holding our first controller, it is located in ‘\helloworld\app\controller\’ folder, and name of the file is ‘say_controller.rb’. Open this file in notepad and type next two lines of code after line with ‘class SayController < ApplicationController’ and before line with ‘end’:
def index
end
6) Now we will create new file, but now we will have to do it manually (no generate scripts for this one). We have to create view which will be related previous controller. To do this, navigate to folder ‘\helloworld\app\say\’ and create new file ‘index.html.erb’ with notepad, and type next line of code:
<h1>Hello World from View</h1>
7) Believe it or not, we are ready to start our application. Because we are dealing with web application we will need some server that will accept calls from web browser. There is one easy solution for this, it is WEBrick server. To start WEBrick server, execute next line in command prompt:
ruby script/server
8) After previous step you will see some text in command prompt which describes what just happened. To see results in web browser, navigate to next url:
9) You should see ‘Hello World’ message on the screen.
Now we will dissect previous steps, and talk a little bit more about each one of them.
Steps 1-3 are needed to create structure of rails application. By structure I mean nested folders inside application folder (in our case named ‘helloworld’). If you look inside application folder you will see plenty of folders. Each one of them is for particular purpose. For now we only used ‘app’ folder, but there are more which I will try to talk about next time.
We created rails application using just command prompt and notepad, but all this could be done inside some IDE like NetBeans, but for start its much better to do it this way using nothing more then command prompt, windows explorer and notepad.
Note that in step 3, we had to get inside application folder, which is because all ‘ruby’ prefixed commands in next steps will have results on application inside application folder.
In step 4, we created controller class by executing ‘ruby script/generate’ command and specifying what type of class we want to generate (controller) and its name (say). This as a results has creation of file ‘say_controller.rb’ inside ‘\app\controllers\’ folder, and also folder ‘\app\views\say’ (beside this two mentioned things there are few more files generated, but about them some next time). So we have some clue about how things are organized here. There is folder for keeping controller, views and models (little bit later) together, and it may sounds familiar to pattern named MVC (Model-View-Controller). Rails applications are organized in that way.
In step 5, we entered first lines of our code, and they defined a method inside controller class. So just to mention this one too, controller class is SayController, it is inside say_controller.rb file, and we generated it by using ‘say’ token, so you see convention on work. There will be more things like this, when convention acts in order to simplify things and speed up development. SayController is extending class ApplicationController, all controllers does that.
It might be strange that even we created empty ‘index’ method, application rendered correctly ‘Hello World’ message in browser, how is that? It is because of the view we created later. So view is inside ‘\app\views\say\’ folder, this way rails knows it is related to ‘say’ controller. Name of the view is ‘index.html.erb’, so once again we see some convention, method name was ‘index’ and it is how view is named too. There is also double extension ‘.html.erb’, the shortest explanation for this is that it is a mixture of html code and embedded ruby code (ERB). And because its not plain html file (its view template) it has this kind of extension.
The content of view file is ‘<h1>Hello World from View</h1>’. Hmm, this one is very plain html code, but it’s just a hello world application, we will see soon how to mix html and ruby.
Step 7 fires up WEBrick server. It is integrated server that comes with rails and its very useful in development process. Note that after executing ‘ruby script/server’, command prompt freezes. It is because server process is holding that command prompt blocked for new commands. Unless we use ‘ctrl+c’ key combination to stop server, this command prompt is useless. So it is the best to start with two command prompts, one for manipulating with application and other reserved for server. There are other servers too, one of them that is simple also is Mongrel.
When we have server ready for accepting requests we can open web browser and enter url ‘http://localhost:3000/say’. From this url we see that we are trying to connect on localhost machine (our machine) at the 3000 port number (its port on which WEBrick is working on), and we are requesting ‘say’. How this is translated: WEBrick receives request, delegates it to the rails engine which is parsing url and creating map of values. In this case there is just one token ‘say’ which rails will translate as a name of controller. So it will call something inside SayController, some method (or action in rails term), but we haven’t supplied any particular action in url, so by convention ‘index’ will be called and we created that one inside controller. ‘index’ method is executed and right after that rails search for view related to ‘index’ action and finds ‘index.html.erb’ which renders hello message. And that’s it, the easiest hello world rails application.