Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use @ in a Rails View and when to use a symbol?

  <% form_tag(:action=>'update', :id=>@album.id) do %>

  Title: <%= text_field(:album, :title)  %><br>
  Artist: <%= text_field(:album, :artist)  %><br>
  Genre: <%= text_field(:album, :genre)  %><br>
  Release Date: <%= datetime_select(:album, :release_date, :start_year=>1960) %><br>

  <%= submit_tag("Update") %>

  <% end %>

In the example above, which works, I had to say @album.id in the form_tag but in the text_field I had to say :album. I keep confusing these in my views and never really know whether to use an @ sign or a symbol. Is there a simple rule that could make it clear what to use when?

like image 321
pez_dispenser Avatar asked May 08 '09 03:05

pez_dispenser


People also ask

When should you use a symbol to store text and when should you use a string?

One rule of thumb is that if the text at hand is “data”, then use a string. If it's code, then use a symbol, especially when used as keys in hashes (see below). Another way of looking at symbols is that they aren't really text, even though they read well. Instead they are unique identifiers, like numbers, or bar codes.

What are the differences between a string and a symbol?

What is the difference between a symbol and a string? A string, in Ruby, is a mutable series of characters or bytes. Symbols, on the other hand, are immutable values. Just like the integer 2 is a value.

Why do we use symbols in Ruby?

Ruby symbols are defined as “scalar value objects used as identifiers, mapping immutable strings to fixed internal values.” Essentially what this means is that symbols are immutable strings. In programming, an immutable object is something that cannot be changed.

What does :: mean in Ruby?

The :: is a unary operator that allows: constants, instance methods and class methods defined within a class or module, to be accessed from anywhere outside the class or module. Remember in Ruby, classes and methods may be considered constants too.


1 Answers

I'm going go off on a tangent here for a moment, but bear with me.

Firstly, I recommend that you use RESTful routing as this gives you access to some of Rails better methods and operations such as being able to do:

form_for(@album) do |f|

and having it work out where you want to go based on the #new_record? state of that object.

Secondly, with the new form_for in place, you'll be able to DRY up your views by doing:

<%= f.text_field :title %>

instead of:

<%= text_field :album, :title %>

And finally the explanation of why a variable is defined with an @ sign before it in Rails, also known as "instance variables":

When you define an instance variable in Rails it is available inside that instance for the entire request where the "instance" is the ActionController and ActionView chain of methods that get called to do all the rendering and so on for you. Defining it as an instance variable will make it available in your controller, any method you call after defining it in the controller, your helpers, your views and the partials rendered from either your controllers, helpers or views.

Basically, it's around for the entire request but is not accessible inside your model.

Other variable specifications are class variables (@@some_useful_thing = 1) and global variables ($some_other_useful_thing = 1) and constants (ALL_IN_UP_CASE = 1).

like image 56
Ryan Bigg Avatar answered Sep 28 '22 07:09

Ryan Bigg