# How to pass an object to the view

**URL:** <https://www.ruby-forum.com/t/how-to-pass-an-object-to-the-view/107901>\
**Category:** Rails\
**Created:** [June 27, 2007, 8:02am UTC](https://www.ruby-forum.com/t/how-to-pass-an-object-to-the-view/107901 "2007-06-27T08:02:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ayyanar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@ayyanar](https://www.ruby-forum.com/u/ayyanar)\
**Post date:** [June 27, 2007, 8:02am UTC](https://www.ruby-forum.com/t/how-to-pass-an-object-to-the-view/107901/1 "2007-06-27T08:02:20Z")

</div>

Hi,

I created the ArithmeticDisplayBean

class ArithmeticDisplayBean  
attr\_accessor :result  
end

This is the controler

class RubyApplicationController \< ApplicationController  
@arithmeticDisplayBean  
def arithmetic  
if params[:paramaction] == ‘save’  
arithmetic = Arithmetic.new  
arithmeticDisplayBean = ArithmeticDisplayBean.new

```
  arithmeticDisplayBean.result =

```

arithmetic.add(params[:num1].to\_i,params[:num2].to\_i)  
end  
end  
end

How can I get the result from the arithmeticDisplayBean.result in the  
view? Can anyone help?

---

<div class="post-metadata">

**Author:** ![ayyanar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@ayyanar](https://www.ruby-forum.com/u/ayyanar)\
**Post date:** [June 27, 2007, 8:47am UTC](https://www.ruby-forum.com/t/how-to-pass-an-object-to-the-view/107901/2 "2007-06-27T08:47:22Z")

</div>

you can use every instance variable you declare in the controller  
instance in the view.

if you define result like this:

@result = arithmeticDisplayBean.result

the will display it if you write this:

\<%= @result %\>

---

<div class="post-metadata">

**Author:** ![ayyanar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@ayyanar](https://www.ruby-forum.com/u/ayyanar)\
**Post date:** [June 27, 2007, 8:54am UTC](https://www.ruby-forum.com/t/how-to-pass-an-object-to-the-view/107901/3 "2007-06-27T08:54:22Z")

</div>

Comments are inline

On 6/27/07, ayyanar [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This is the controler
> 
> class RubyApplicationController \< ApplicationController

@arithmeticDisplayBean

This is an instance variable of the class, not the instance. It is  
accessible through class methods, not instance methods. You should be  
able  
to remove this line.

def arithmetic

> ```
> if params[:paramaction] == 'save'
> arithmetic = Arithmetic.new
> arithmeticDisplayBean = ArithmeticDisplayBean.new
> 
> ```

This line  
arithmeticDisplayBean = ArithmeticDisplayBean.new  
loads your Bean into a local variable. Local variables are not  
available in  
your views. This should be

@arithmeticDisplayBean = ArithmeticDisplayBean.new

```
  @arithmeticDisplayBean.result =

```

> arithmetic.add(params[:num1].to\_i,params[:num2].to\_i)  
> end  
> end  
> end
> 
> How can I get the result from the arithmeticDisplayBean.result in the  
> view? Can anyone help?

If you use the instance variable then it will be available in your view.

Alternatively you can just stick the result into an instance array on  
the  
fly

@result = arithmeticDisplayBean.result =  
arithmetic.add(params[:num1].to\_i,params[

> :num2].to\_i)

HTH  
Daniel
