@ variables in Ruby on Rails
Use @title
in your controllers when you want your variable to be available in your views.
The explanation is that @title
is an instance variable while title
is a local variable. Rails makes instance variables from controllers available to views because the template code (erb, haml, etc) is executed within the scope of the current controller instance.
title
is a local variable. They only exists within its scope (current block)
@title
is an instance variable - and is available to all methods within the class.
You can read more here: http://strugglingwithruby.blogspot.dk/2010/03/variables.html
In Ruby on Rails - declaring your variables in your controller as instance variables (@title
) makes them available to your view.
A tutorial about What is Variable Scope? presents some details quite well, just enclose the related here.
+------------------+----------------------+
| Name Begins With | Variable Scope |
+------------------+----------------------+
| $ | A global variable |
| @ | An instance variable |
| [a-z] or _ | A local variable |
| [A-Z] | A constant |
| @@ | A class variable |
+------------------+----------------------+