3.7. Misc

You have now already created a simple Rails application and in the next chapter you will dive deeply into the topic ActiveRecord. So now is a good time to very briefly introduce a few terms that often surface in the world of Rails.

"Model View Controller" Architecture (MVC)

According to Wikipedia (http://en.wikipedia.org/wiki/Model–view–controller), MVC is a design pattern that separates the representation of information from the user's interaction with it.
MVC is a structure for software development. It was agreed that it makes sense to have one part of the software in one place and another part of the software in another place. Nothing more, nothing less. This agreement has the enormous advantage that once you are used to this concept, you know exactly where you can find or need to integrate a certain functionaity in a Rails project.


"Model" in this case means data model. By default, Rails applications are an ActiveRecord data model (see Chapter 4, ActiveRecord).
All models can be found in the directory app/models/.


The "view" is responsible for the presentation of the application. It takes care of rendering the web page, an XML or JSON file. A view could also render a PDF or an ASCII text. It depends entirely on your application.
You will find all the views in the directory app/views/.


Once a web page call has ended up in a route (see Chapter 6, Routes), it goes from there to the controller. The route specifies a certain method (action) as target. This method can then fulfil the desired tasks (such as finding a specific set of data and saving it in an instance variable) and then renders the desired view.
All controllers can be found in the directory app/controllers/.

What is a Generator?

We have already used the command rails generate controller. It starts the generator with the name controller. There are other generators as well. You can use the command rails generate to display a list of available generators:
$ rails generate
Usage: rails generate GENERATOR [args] [options]

General options:
  -h, [--help]     # Print generator's options and usage
  -p, [--pretend]  # Run but do not make any changes
  -f, [--force]    # Overwrite files that already exist
  -s, [--skip]     # Skip files that already exist
  -q, [--quiet]    # Suppress status output

Please choose a generator below.






What does a generator do? A generator makes a programmer's job easier by doing some of the mindless tasks for you. It creates files and fills them with content, depending on the parameters passed. You could do the same manually, without the generator. So you do not have to use a generator. It is primarily intended to save you work and avoid potential errors that can easily arise from mindless repetitive tasks.

Buy the new Rails 5.1 version of this book.

Someday you might want to create your own generator. Have a look at http://guides.rubyonrails.org/generators.html to find a description of how to do that.


A helper method takes care of recurring tasks in a view. For example, if you want to display stars (*) for rating a restaurant and not a number from 1 to 5, you can define the following helper in the file app/helpers/application_helper.rb :
module ApplicationHelper

  def render_stars(value)
    output = ''
    if (1..5).include?(value)
      value.times { output += '*'}

With this helper, we can then apply the following code in a view:
<b>Rating:</b> <%= render_stars(5) %>
You can also try out the helper in the console:
$ rails console
Loading development environment (Rails 4.0.0)
>> helper.render_stars(5)
=> "*****"
>> helper.render_stars(3)
=> "***"
>> exit
There are lots of predefined helpers in Rails and we will use some of them in the next chapters. But you can also define your own custom helpers. Any of the helpers from the file app/helpers/application_helper.rb can be used in any view. Helpers that you want to be only available in certain views must be defined for each controller. When creating a controller, a file for helpers of that controller is automatically created in app/helpers. This gives you the option of defining helpers only for this controller or for the views of this controller.
All helpers are in the directory app/helpers/.

Rails Lingo

Here you find a couple of words which you'll often find in the Ruby on Rails universe.

DRY - Don't repeat yourself

Many Rails programmers are big fans of DRY. DRY means purely and simply that you should try to place repeated programming logic into separate methods.


You often hear the word refactoring in the context of DRY. This involves functioning applications that are further improved. The application in itself remains unchanged in its interface. But its core is optimized, amongst others through DRY.

Convention Over Configuration

Convention over configuration (also known as coding by convention, see http://en.wikipedia.org/wiki/Convention_over_configuration) is an important pillar of a Rails application. It states that the programmer does not need to decide in favour of certain features when starting a project and set these via configuration parameters. It specifies an underlying basic consensus and this is set by default. But if you want to work outside of this conventional basic consensus, then you will need to change the corresponding parameters.