In last post we have seen how to install ruby on rails .Here we are going to print "Hello world" and see folder structure
One of the important feature of Ruby on Rails is it rely on Model-View-Controller architecture (MVC). The main advantage of MVC is separation of Business logic from user interface.
Model : Model will stores the information about each table of a database. It also do some primary validation of data before storing into database.
View : View is generally the interface of your application. Views are often HTML pages with some embedded ruby code. Views handle the job of providing the data to web browser
Controller : Controller mainly handles the incoming requests from the web browser, interrogating the models for data, and passing that data on to the views for presentation.
Now Lets start creating new project
1) Goto Instant Rails folder and open InstantRails.exe
2) Click on Ruby on rails icon and Rails Applications -> Open Ruby Console Window.
3) Now Ruby console window is opened create a new project using command rails welcome.
4) Now the required files are created by accomplishing the above command.
5) Change to welcome directory by typing cd welcome
6) Now we are in welcome directory. To start WEB Server type ruby script/server(Make sure that you are in the welcome directory before starting the server).
7) To confirm server is running or not, open browser and try http://localhost:3000, then you should see the welcome screen.
Up-to this we have seen in previous post . Now lets create controller
8) Now leave the Web Server running (do not close the console) open a new console window as described in step2 and locate to welcome directory by typing cd welcome
9) This is the important step as we are going to create a Controller. To create a controller called say type ruby script/generate controller say.
10) Now goto welcome->app->controllers and open say_controller.rb
11) The contents of say_controller.rb will be
12) Define an actions in say_controller.rb called hello and the code will be
13 ) Now goto welcome/app/view/say/ and create a file called hello.html.erb with some html content.
14 ) Now try to access http://localhost:3000/say/hello/. You will see the output for hello.html.erb
15) See the following diagrams illustrating the url mapping and structure of controllers.
Finally see the Directory Structure descritpion
When you are creating a ruby project with help of helper scripts, it generates the required files and directories required for an application. The directory structure and naming conventions for every project is same and here is the some brief introduction of each directory.
It contains all the code of that particular project. It also contains 3 three main folders which reflects the MVC architecture.
It contains the controller classes. These controllers mainly handles web requests from users.
This directory contains the classes which will model and grab the data stored in application’s database.
The view subdirectory holds the display templates to fill in with data from our application, convert to HTML, and return to the user’s browser.
Holds the template files for layouts to be used with views. This models the common
header/footer method of wrapping views. In your views, define a layout using the
layout :default and create a file named default.html.erb. Inside default.html.erb,
call <% yield %> to render the view using this layout.
The helpers directory holds the helper classes and these classes assist the model, view, and controller classes.
Configuration files for the Rails environment (environment.rb), database configuration (database.yml) and routing of incoming user requests (routes.rb).
Contains the database schema in schema.rb. You can manage the relational database with scripts you create and place in this directory
This directory is where your application documentation will be stored. This documentation can automatically generated using the command rake doc:app
Contains the application libraries. Generally the code which doesn’t belongs to controllers, models, helpers is stored here and this directory is set in load path.
The directory available for the web server. Contains subdirectories for images, stylesheets,
and java scripts.
Helper scripts for automation and generation. (example launching a web server)
Unit and functional tests along with fixtures. When using the script/generate scripts, template
test files will be generated for you and placed in this directory.
External libraries such as third party libraries are stored here.