Howcome an Instance variable @variable defined in the controller's action can be called from its views?

Go To StackoverFlow.com

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
2012-04-05 19:54
by Manish Shrivastava
I meant to ask how @variable is accessible in VIEWS defined in controller's action.. - Manish Shrivastava 2012-04-05 20:07
Possible duplicate of How are Rails instance variables passed to views?lulalala 2016-03-03 02:51
I asked this question in 2012. so " How are Rails instance variables passed to views?" That question is duplicate of my question - Manish Shrivastava 2016-03-04 12:14
Sorry I see how it is unfair, but it has a better answer so... http://meta.stackexchange.com/questions/10841/how-should-duplicate-questions-be-handle - lulalala 2016-03-07 02:11


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.

2013-07-30 11:05
by Bornfree


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.

2012-04-05 21:09
by Jesse Wolgamott


1

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

2012-04-05 21:29
by Luca G. Soave


1

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

2012-04-06 01:09
by Kyle C
my question is just WHY SO? (Instance variables assigned in that action are available in that view. - Manish Shrivastava 2012-04-06 08:47
I found this short description helpful, http://www.devarticles.com/c/a/Ruby-on-Rails/Rails-Action-Controller - Kyle C 2012-04-06 15:24
So to answer your "How" question, "Action Controller includes a routing module that maps URLs to specific actions - Kyle C 2012-04-06 15:33
I reached upto "action", now how "views"'s index.html.haml - Manish Shrivastava 2012-04-07 06:39
can you elaborate on "how "views"'s index.html.haml ?" , not sure what you mean by that - Kyle C 2012-04-07 16:17


0

The haml syntax for this would be:

-@whythiscolavery.each do |user|
  = user.name

The erb syntax would be

<%= @whythiscolavery.each do |user| %>
  <%= user.name %>
<% end %>
2012-04-05 19:57
by niiru


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

2012-04-05 20:50
by mongo296
Ads