How to pass an object to the view


#1

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?


#2

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 %>


#3

Comments are inline

On 6/27/07, ayyanar 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