1

How and instance variable @variable defined in the controller's action can be called from its views ?

like

class UsersControllers
  def index
     @whythiscolavery=User.all
  end
end

Now in /views/users/index.html.haml why variable @whythiscolavery is directly accessible in views defined in controllers action?

This query failed me in an important interview..

@whythiscolavery.each do   
     user.name
 end
4
  • I meant to ask how @variable is accessible in VIEWS defined in controller's action..? Commented Apr 5, 2012 at 20:07
  • Possible duplicate of How are Rails instance variables passed to views? Commented Mar 3, 2016 at 2:51
  • I asked this question in 2012. so " How are Rails instance variables passed to views?" That question is duplicate of my question. Commented Mar 4, 2016 at 12:14
  • Sorry I see how it is unfair, but it has a better answer so... meta.stackexchange.com/questions/10841/… Commented Mar 7, 2016 at 2:11

6 Answers 6

4

Quoting Obie Fernandez from The Rails 3 way:

The way Rails implements controller-to-view data handoffs is through instance variables. Typically, a controller action initializes one or more instance variables. Those instance variables can then be used by the view.

There’s a bit of irony (and possible confusion for newcomers) in the choice of instance variables to share data between controllers and views. The main reason that instance variables exist is so that objects (whether Controller objects, String objects, and so on) can hold on to data that they don’t share with other objects. When your controller action is executed, everything is happening in the context of a controller object—an instance of, say, DemoController or EventController. Context includes the fact that every instance variable in the code belongs to the controller instance.

When the view template is rendered, the context is that of a different object, an instance of ActionView::Base. That instance has its own instance variables, and does not have access to those of the controller object.

So instance variables, on the face of it, are about the worst choice for a way for two objects to share data. However, it’s possible to make it happen—or make it appear to happen. What Rails does is to loop through the controller object’s variables and, for each one, create an instance variable for the view object, with the same name and containing the same data.

It’s kind of labor-intensive, for the framework: It’s like copying over a grocery list by hand. But the end result is that things are easier for you, the programmer. If you’re a Ruby purist, you might wince a little bit at the thought of instance variables serving to connect objects, rather than separate them. On the other hand, being a Ruby purist should also include understanding the fact that you can do lots of different things in Ruby—such as copying instance variables in a loop. So there’s nothing really un-Ruby-like about it. And it does provide a seamless connection, from the programmer’s perspective, between a controller and the template it’s rendering.

Sign up to request clarification or add additional context in comments.

Comments

2

Views are basically methods of a controller, so they can access instance variables just like a regular plain old ruby class can. If you had a class

class Donut
  def show
    @delicious = "very very delicious"
  end

  private
  def render_show
    "donuts are #{@delicious}"
  end
end

Donut.new.show

Erb (and haml, and js and all other renders) are handled in the same way.

Comments

1

Because in Ruby On Rails MVC architecture and "convention over configuration", instance variables are crossing models/views/controllers and helpers.

Comments

1

methods in a controller are public actions (unless they are protected). Instance variables assigned in that action are available in that view.

5 Comments

my question is just WHY SO? (Instance variables assigned in that action are available in that view.)
I found this short description helpful, devarticles.com/c/a/Ruby-on-Rails/Rails-Action-Controller
So to answer your "How" question, "Action Controller includes a routing module that maps URLs to specific actions"
I reached upto "action", now how "views"'s index.html.haml ?
can you elaborate on "how "views"'s index.html.haml ?" , not sure what you mean by that.
0

The haml syntax for this would be:

[email protected] do |user|
  = user.name

The erb syntax would be

<%= @whythiscolavery.each do |user| %>
  <%= user.name %>
<% end %>

Comments

0

if you're asking how to access the variable from the view, niiru's answer is good with the exception of an extra '=' sign in the erb syntax example. it should be:

<% @whythiscolavery.each do |user| %>
  <%= user.name %>
<% end %>

if you're asking why it is possible that the variable is available in the view, it's because the controller makes them available to the view (they're copied to the view). this design shortcoming (maintaining state) prompted the creation of the gem decent_exposure: https://github.com/voxdolo/decent_exposure

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.