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

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.

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.

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs