This content has been automatically translated from Ukrainian.
ivar - is short for instance variable (instance variable). In Ruby, she records with @ before the title, 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 controllers and @user in views - it's the same variable within one request, but she belongs to a specific instance of the controller.
Rails automatically makes all @-variables from the controller available in the appropriate template.controller:
# app/controllers/users_controller.rb def show @user = User.find(params[:id]) end
same puff:
<!-- app/views/users/show.html.erb --> <h1><%= @user.name %></h1>
In short:
- @ivar = instance variable = object instance variable
- Visibility within limits one copy
- Rails automatically transmits @-variables from the controller to view
- Used to transfer data between MVC layers
This post doesn't have any additions from the author yet.