# Passing data to a view: How prevent the instance variables?

**URL:** <https://www.ruby-forum.com/t/passing-data-to-a-view-how-prevent-the-instance-variables/126872>\
**Category:** Rails\
**Created:** [January 23, 2008, 12:33am UTC](https://www.ruby-forum.com/t/passing-data-to-a-view-how-prevent-the-instance-variables/126872 "2008-01-23T00:33:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![i72gumaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/9fc29f/32.png) [@i72gumaj](https://www.ruby-forum.com/u/i72gumaj)\
**Post date:** [January 23, 2008, 12:33am UTC](https://www.ruby-forum.com/t/passing-data-to-a-view-how-prevent-the-instance-variables/126872/1 "2008-01-23T00:33:35Z")

</div>

Greetings, i want pass data to my view without using instace variables.  
Usually I make renders to partials for pass local variables and  
preventing use instace variables :

Example:

render( :partial =\> “filename”, :locals =\> {:paramName = @anyVar} )

I’ve this method in my controller(controller.rb):

def anyMethod

var1=5  
var2=6

end

There’s any way to pass var1 and var2 to the default view  
anyMethod.rhtml without use instace variables?

Thanks for any answer, Juan P.

---

<div class="post-metadata">

**Author:** ![i72gumaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/9fc29f/32.png) [@i72gumaj](https://www.ruby-forum.com/u/i72gumaj)\
**Post date:** [January 23, 2008, 11:56am UTC](https://www.ruby-forum.com/t/passing-data-to-a-view-how-prevent-the-instance-variables/126872/2 "2008-01-23T11:56:44Z")

</div>

I have to ask… why are you so keen on avoiding instance variables?  
Sorry, I am just curious because they are so widely the standard way of  
passing data between controller and view. I actually don’t know of any  
other way but that might be simply that I never had a need.

---

<div class="post-metadata">

**Author:** ![i72gumaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/9fc29f/32.png) [@i72gumaj](https://www.ruby-forum.com/u/i72gumaj)\
**Post date:** [January 23, 2008, 3:26pm UTC](https://www.ruby-forum.com/t/passing-data-to-a-view-how-prevent-the-instance-variables/126872/3 "2008-01-23T15:26:21Z")

</div>

Nathan E. wrote:

> I have to ask… why are you so keen on avoiding instance variables?  
> Sorry, I am just curious because they are so widely the standard way of  
> passing data between controller and view. I actually don’t know of any  
> other way but that might be simply that I never had a need.

I just want work in a very modular way (OO mode). And I don’t need than  
all my variables have to be with public access, that’s the reason

---

<div class="post-metadata">

**Author:** ![i72gumaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/9fc29f/32.png) [@i72gumaj](https://www.ruby-forum.com/u/i72gumaj)\
**Post date:** [January 23, 2008, 6:34pm UTC](https://www.ruby-forum.com/t/passing-data-to-a-view-how-prevent-the-instance-variables/126872/4 "2008-01-23T18:34:01Z")

</div>

> I just want work in a very modular way (OO mode). And I don’t need than  
> all my variables have to be with public access, that’s the reason

There is nothing wrong with passing instance variables to the view.  
However, I do think its good practice to limit the number of variables  
you use. If your passing a large number of variables they can  
probably be wrapped into an object, then you can just pass the  
object. For example:

def some\_controller\_method  
@height = 5  
@width = 10  
@depth = 2  
end

compared to:

def some\_controller\_method  
@box = Box.new(:height =\> 5, :width =\> 10, :depth =\> 2)  
end

Then your view calls @box.height, etc…

Aaron
