This content has been automatically translated from Ukrainian.
ivar is short for instance variable. In Ruby, it is written with a @ before the name, for example:
@user = User.find(params[:id])
How does it work?
In Ruby, each object has its own set of instance variables. That is, @user in the controller and @user in the view are the same variable within a single request, but it belongs to a specific instance of the controller.
Rails automatically makes all @-variables from the controller available in the corresponding template.controller:
# app/controllers/users_controller.rb def show @user = User.find(params[:id]) end
and the same view:
<!-- app/views/users/show.html.erb --> <h1><%= @user.name %></h1>
In short:
- @ivar = instance variable = instance variable of an object
- Visibility within a single instance
- Rails automatically passes @-variables from the controller to the view
- Used to pass data between MVC layers
This post doesn't have any additions from the author yet.