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
).