Rails Basics (MVC, Controllers, Views and Routes)
from terminal go to the place where you want your project to be and type:
$rails new [name_of_the_application] -d mysql
Note: -d mysql for preconfiguring with MySQL. Without it it will default to SQLLite.
Rails generates all the necessary files for us.
Before seeing the results on through the web server we will need to launch a web server. We can use the one that is bundled with the rails, called WebBrick.
$rails server (or) $rails s
The server is going to start up and use port 3000 by default. You can see your content @
localhost:3000 (or) 127.0.0.1:3000
MVC Architecture
We are going to be looking at the first three components of it: Browser->Controller->View
Use rails command to generate the controller. From the root of your project execute the following command:
$rails generate controller
This will bring out a help page about the controller and how to generate one
$rails generate controller demo index
Will create a controller called demo and a view that is going to be associated with it called demo.
This will output all the files that were created for you.
Note: Method inside a controller is called an action. It is still a method, but we call it an action.
In the demo controller file, inside your project ([app_name]/app/controllers/demo_controller.rb) there is a method (action) called index. There is also a view with the same file name as an action in the views directory under the folder name with the name of the controller - "demo" ([app_name]/app/views/demo/index.html.erb).
Files Structure of Rails Application
app | |-controllers |-helpers (helper code used by controllers, views and models) |-models |-views config | |-application.rb |-boot.rb |-database.rb |-environment.rb |-locales (localization files for different language) |-routes.rb (how different urls get send to the different parts of the application) config.ru (leave it for now) db (db migration code) doc (documentation for our app, todo lists and such) Gemfile (for 3rd party code, called gems) lib (for 3rd party libraries) log public (files that do not need to be processed by the framework) | |-404.html |-422.html |-500.html |-favicon.ico |-images |-index.html |-javascript |-robots.txt |-stylesheets Rakefile (leave it, used by Rails) README (brief description of your application) script (scripts used by Rails, rails command is in it right now) test (for unit tests) temp (for Rails to put temporary files to) vendor | |-plugins (for 3rd party plugins)
Second take on Rails MVC
First thing the Web Server does is checks in the public directory if the file is there that exactly matches the request from the browser, without accessing the framework.
If the file is not found, the request is going to go to the Rails framework and check the Routing to determine which controller and action to use based on the request URL.
The result is the same - an html page.
Note: the public directory is in the way of the request. So if the file name which matches the request is in that directory it's content is going to be return to the browser, even though we might have meant to execute a controller action instead.
Second Note: the web server is setup the way that ".html" is the default file extension, so request to index and index.html will be treated identically and will return the content of the index.html back to the browser.
Routes
In your routes.rb file in the config folder you will see a generated simple route and a bunch of examples on how to write complex routes.
get "demo/index" is equivalent to match "demo/index", :to => "demo#index"
It's just a one to one match.
Default Route
match ':controller(/:action(/:id(.:format)))'
":" indicates that it is a Ruby symbol. This will route all the url request to the actions under the controllers that they belong to. And will pass the id of the db record to it if needed.
E.g: http://yourapp.com/activity/show/53 will be routed to the action called "show" in the "activity" controller and will pass 53 as a parameter.
Root Route
root :to => "demo#index"
Is used for the routing requests to the root of the application.
E.g.: http://yourapp.com/ will be routed to index action of the demo controller.











