From: Reacher Date: 2007-12-07T00:23:23+09:00 Subject: Re: how(where) to define a method that to be called by views? On Dec 6, 9:02 am, "Sun" 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:

<%= @somevar %>

[/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:

<%= somevar %>

[/code]