What is the scope of an instance variable in a Rails controller? - ruby-on-rails

If I create an instance variable in a Rails controller, what is the scope of that instance variable? Is it available to all of the Rails application, or just to the views and models associated with that particular controller? Since I am new to Rails, I am a bit confused.

An instance variable in a Rails controller is used in two ways:
It is available within instance methods of that controller (including superclass instance methods), just like any other Ruby instance variable.
When a Rails controller action renders a view, the controller's instance variables are copied (shallowly) to the view instance. (The view instance is the value of self within a template.) Controller instance variables defined at the time an action renders are therefore effectively available within that action's view. View helpers are just modules which are extended by the view instance, so controller instance variables are effectively available within view helper methods too.
Note that although controller instance variables are copied to the view, instance variables defined in the view are not copied back to the controller. That's not something you'd normally need to have happen because rendering the view is normally the last thing done in a controller action. And local variables defined in a view are available throughout the view, so there's no reason at all to define an instance variable in a view.
An instance variable in a given controller is not available within other controllers, within other controllers' views, or within any models at all.

Related

where can i find the class definition for action controller base?

I am just a bit curious how application controller and action controller objects work in rails. Is there anywhere I can view the complete class templates for these objects? I was wondering if methods like "show" and "index" are just hook methods that are called inside the initialize of the object. Is this why we define our instance members from these? Are they basically just empty methods called in the init that will then define the instance variables as the objects are selected from the routes.rb file?

Which class, specifically, do view helpers get mixed into?

Everything I read says that view helpers get mixed into views, but which class, specifically do they get mixed into?
References:
http://guides.rubyonrails.org/getting_started.html#view-helpers
Why can private helper methods still be accessed in views?
Do helper classes get mixed into the controller?
The controller has a view_context, which is an instance of view_context_class, which is (by default) an anonymous subclass of ActionView::Base created by ActionView::Base.prepare. The helpers are mixed in to these view context classes.
The view context is also the place where the controller instance variables "magically" become instance variables in the view.

Rails: How can an instance variable declared in a controller accessible in a view?

Ruby instance variables are accessible to a single object. But in rails, if I declare an instance variable in a controller, it is still accessible in the views. What is the architecture behind this?
Well, your controller calls render which renders your templates. So, the template code is being run within the scope of the controller instance. Therefore, you can use any instance variables declared.

Instance variable vs method calling in Controller

I have the following code in my controller.
def index
#customer = Customer.find(params[:customer_id])
#current_orders = #customer.current_orders
#pending_orders = #customer.pending_orders
#previous_orders = #customer.previous_orders
#recurring_orders = #customer.recurring_orders
end
As you see, all the instance variables(#current_orders,#pending_orders,etc) can be obtain from #customer.
What is the best way to write this code?
Should I need to create these instance variable in controller or I just only used these through #customer variable in views.
If you reference the customer methods directly in the view you are tying the view directly to your model i.e. if for some reason you needed to change the name of the previous_orders method on customer you'd have to go through all your views and change it there, whereas with the instance variables you've used you'd only have to change it in your controllers. Using instance variables also makes your views more re-usable.
However, if you find yourself using loads of instance variables in your controllers it may be worth investigating adding another layer of abstraction in there to tidy things up.
In my opinion the example you've given is fine.

In Ruby on Rails, how is view code able to use instance variables that are set inside the controller code?

In Ruby on Rails, how is an ActionView object able to access the instance variables of ActionController object (how is view code able to use instance variables that are set inside the controller code)?
It isn't. The controller creates fresh instance variables inside the view object, based on the instance variables in the controller

Resources