hyena
12/6/2007 3:22:00 PM
"Reacher" <brandon.g.jones@gmail.com> wrote in message
news:f967ff7f-e2c6-43f0-8cc3-571af0151eb4@x69g2000hsx.googlegroups.com...
> On Dec 6, 9:02 am, "Sun" <a...@hut.at> wrote:
>> Hi,
>>
>> I just dabbed Ruby on rails abit and what to know where is the right
>> place
>> to define a method, that prinf some output information to views, based on
>> some variable.
>>
>> I define a function "def foo " in contrioller but the calling from a view
>> "test.rhtml" give errors. I google a while and did not find a quick
>> answer
>> for this. Or shall I do this in helper class?
>>
>> May some one shed some light on this?
>>
>> Thanks.
>
> You'll need to post some code. It's basically done like this:
> [code]
> app/controllers/my_controller.rb:
> class MyController < ActiveController::Base
> def index
> @somevar = 'Hi!'
> end
> end
>
> app/views/my_controller/index.rhtml:
> <p><%= @somevar %></p>
> [/code]
> If you want to call a routine in your view, it's best to put it into
> the appropriate helper class (that's what they are there for)
> [code]
> app/helpers/my_controller_helper.rb:
> module MyControllerHelper
> def somevar
> 'Hi!'
> end
> end
> [/code]
> now your controller/view looks like this
> [code]
> app/controllers/my_controller.rb:
> class MyController < ActiveController::Base
> def index
> end
> end
>
> app/views/my_controller/index.rhtml:
> <p><%= somevar %></p>
> [/code]
thanks you guys for your prompt answers!!!, you are of great help. I am in a
hurry to revise some rails code written by others and am a complete newbie
to either rails or ruby, just borrowed one book and try to understand the
ideas. .