Guest post by Sherri Jones
Ruby is a dynamic, open source programming language with primary focus on simplicity and productivity. What makes Ruby stand out is its elegant syntax that is easy to read and quick to write. Before we start learning Ruby on Rails, it’s important to know what it is about.
Let’s start with the fundamental principles of ruby on rails.
Convention over Configuration (CoC)
CoC translates to “follow these naming conventions and the framework will just figure out what to do by itself.” The “CoC” model promises better productivity by removing much of the repetitive explicit configuration and mapping and allowing developer to specify only unconventional aspects of the application. For example, if there is a class User in the model, by default table name will be users. Consequently, Ruby on Rails conventions lead to less code and less repetition.
Don’t repeat yourself (DRY)
“Don’t repeat yourself” principle is aimed at repeating code as little as possible, making it easier to make changes later on in the development cycle. If the code exists in only one place, it only needs to be changed in one place. Rails provide several facilities, including helpers and libraries, that help in implementing DRY code. DRY code has the information located in a single, noticeable place. For example, using the ActiveRecord module of Rails, we don’t need to specify database columns in class definition. Instead, Ruby on Rails can fetch the table details from the database using the Class name.
REST-style architectures consist of clients and servers. Clients initiate requests to servers; servers process requests and return appropriate responses.
M – Models, which contain Business logic (like fetching, adding or updating database records)
V – Views, which contains the response to be shown to user
C – Controllers, which contains actions to receive requests and their parameters ,process them (through model methods) and send responses to users (through views)
It clearly differentiates the Business logic, views and request processing and helps to make the code DRY. Hence it makes maintenance easier.
Starting web development with Ruby on Rails
Now that you are familiar with the principles and understand that Rails is an MVC framework, let’s learn how to program in Ruby on Rails. Below is a basic walk through on getting started .We will be using Rails3. Assuming that you have everything installed (If not, you can refer http://rubyonrails.org/download), create your first rails application with following instructions on your command prompt.
rails new my_app
What did it just do? It created the basic app for you, along with a complete basic directory structure, as shown above. In the created Basic Directory, app folder is for assets, models, controllers and views, config folder to have all types of configurations, db folder for database details and log folder to contain all types of logs.
Let’s walk through each of the folders that were created in detail:
This will contain the model, view, controller, helpers, mailers of the application. (in Rails 3.0.x) . In Rails 3.1.x though it will have an additional folder named assets where your css, images and js will live.
The config contains configuration settings of your application. Below are some of the most important files that you will need to play with often:
routes.rb: Define the resources of your application and how would the incoming request map to your controllers here.
environment.rb: This is a where it all begins and the first file that is loaded by the rack server.
database:yml: Configure your database here. One for each environment
application.rb: Any pre initialize code before your Rails application is loaded goes here.
config/environments: Folder environments will contain environment specific configuration file that you will need to manage each environment separately. You can certainly define more environments as needed.
Initializes folder contains files that will be loaded just after your rails application is loaded with its gems and plug-in.
db/migrate: As you build your application and add new models or modify existing ones, you can add a migration that will run against your database. All your migrations live in this folder.
db/schema.rb: The schema will have the structure details of the models used in the application.
4. doc: Ruby doc is a ruby framework for creating documentation for your code. You assist ruby doc with commenting in your code and it will auto generate documentation for your code in this folder
5. log: It contains all the log files like development.log, production.log, test.log and server.log. we can write other log files as well in this folder
6. public: The public will have all the static files which will not change like “index.html, images, style sheets, java scripts” used in our application. (up to rails 3.0.x)
The public will have all the static files which will not change like “index.html” used in our application. (From rails 3.1.x)
7. script: Script folders hold files to launch and manage various tools that you will use with your rails application. For instance, you could have a script to generate code and launch the web server.
8. test: Rails foster test driven development. Test folder contains all your fixtures and different test cases like unit, functional, integration and performance tests.
9. tmp: The tmp will contain the temporary files which will be used by our application like “sessions, sockets, cache, pids”.
10. vendor: The vendor will contain the libraries provided by the third party vendors like “plugins” used to add functionalities to your application.
11. README: The README file will contain all the description of the directory structure and the details about rails application
12. Rakefile: Rake is similar to make in UNIX world. You can configure your important tasks here.
13. config.ru: The config.ru file is used by Rack-based servers to start the rails application.
14. Gemfile: All the gems that your rails application uses are listed in the Gemfile.
Terminal Commands to Remember:
cd my_app -> Enter into the generated rails3 app
gem install bundler -> If bundler gem is not installed
bundle check -> To check all the required gems installed or not
bundle install -> To install all the required gems
Generate required set of files for your model / controller / complete set including model and controller, by just running any of the below commands
rails g model user – > It will generate model and required migration using above command
rails g controller users – > It will generate controller and views folder using above command
rails g scaffold user – > It will generate model, migrations, controller with basic CRUD actions and their views using scaffold command
rake db:migrate – > To run migrations which were not run
rails s – > To run server on default port: 3000
rails s -p 3021 -> To run server on port: 3021
rails c -> To RUN CONSOLE
For editing rails files, following Editors or IDEs can be used.
Free editors – Vim, Sublime, Redcar, Gedit(gmate plugins)
Paid editors – Textmate
Paid IDE – RubyMine
Free IDE – Netbeans
Embedding Rails code into HTML:
As we know, static HTML files end with .html extension, Embedded ruby files ends with .html.erb extension in Rails. We can maintain our views as dynamic by using these Embedded ruby(erb) files. From here onwards we mention these Embedded ruby files as erb files.
In erb Files, if something is
written in <% %> –> will be executed as ruby and will not return anything
written in <%= %> –> will be executed as ruby and will return string
Ex: To show 5 numbers each in a new line, in plain html, we need to write as below
Using Embedded ruby, we can show the same as below
<% 1.upto 5 do|n| %>
<li><%= n %></li>
<% end %>
We can add / update appropriate views in app/views to change the content we see in response to the requests.
Sherri Jones is a Web-Developer and Ruby Programmer. She is looking for interesting projects particularly on ruby on rails. For more details visit at idyllic-software.com.