How to pass an object to the view

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?

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

Comments are inline

On 6/27/07, ayyanar [email protected] 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

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