Forum: Ruby on Rails How to pass an object to the view

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
ayyanar (Guest)
on 2007-06-27 10:02
(Received via mailing list)
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?
lanzm (Guest)
on 2007-06-27 10:47
(Received via mailing list)
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 %>
Daniel -. (Guest)
on 2007-06-27 10:54
(Received via mailing list)
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
This topic is locked and can not be replied to.