HiveBrain v1.2.0
Get Started
← Back to all entries
patternrubyrailsCritical

@ variables in Ruby on Rails

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
variablesrubyrails

Problem

What's the difference between @title and title? Since both of them can be variable names. Also, how do I decide which kind of variable I should use? With @ or not?

Solution

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.

Context

Stack Overflow Q#14319347, score: 536

Revisions (0)

No revisions yet.