Ruby on Rails
This file contains brief descriptions of a Ruby on Rails project.
Important Rails Commands
Here a list of the most relevant rails command-line programs organized by task:
Starting a Rails Project: rails
Executing a Rails Project: ruby script\server ( on application directory )
Generating a new Model: ruby script\generate model
Generating a new Controller: ruby script\generate controller
Holds all the code that's specific to this particular application.
Holds controllers that should be named like weblogs_controller.rb for automated URL mapping. All controllers should descend from ApplicationController which itself descends from ActionController::Base.
Holds models that should be named like post.rb.
Most models will descend from ActiveRecord::Base.
Holds the template files for the view that should be named like weblogs/index.erb for the WeblogsController#index action. All views use eRuby syntax.
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.erb. Inside default.erb, call <% yield %> to render the view using this layout.
Holds view helpers that should be named like weblogs_helper.rb. These are generated for you automatically when using script/generate for controllers. Helpers can be used to wrap functionality for your views into methods.
Configuration files for the Rails environment, the routing map, the database, and other dependencies.
Contains the database schema in schema.rb. db/migrate contains all the sequence of Migrations for your schema.
This directory is where your application documentation will be stored when generated using rake doc:app
Application specific libraries. Basically, any kind of custom code that doesn't belong under controllers, models, or helpers. This directory is in the load path.
Helper scripts for automation and generation.
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 that the application depends on. Also includes the plugins subdirectory. This directory is in the load path.
How does Model, View and Controller relate to each other ?
The application directory is structured like below:
The fastest way to generate a complete crud for a model is to generate a controller with the scaffold option:
> script/generate scaffold blog title:string content:text date_created:datetime
After understanding of ruby-on-rails it is considered better practice to generate models, views and controllers separately:
To generate a blog controller, one must type:
> script/generate controller blog
Result: a BlogController class will be generated at app/controllers in BlogController.rb
To generate a blog model, one must type:
> script/generate model blog
Result: a Blog class will be generated at app/model in blog.rb
views can not be generated, you have to go to app/views/blog and create a blog.html.erb.
Views for BlogController are automatically assigned in app/views/blog by name convention. ( Since BlogController will have a blog directory in app/views ).
Views in app/views/blog, must have a *.html.erb extension and an index.html.erb must be created for initial page. Other auxiliary pages can be created in the same directory with different names.
In order to add/remove/update models fields, one must only update the corresponding table in the data model only. After that the following command should be executed to update the models in Ruby-on-Rails:
> rake db:migrate