# How(where) to define a method that to be called by views?

**URL:** <https://www.ruby-forum.com/t/how-where-to-define-a-method-that-to-be-called-by-views/122315>\
**Category:** Ruby\
**Created:** [December 6, 2007, 4:07pm UTC](https://www.ruby-forum.com/t/how-where-to-define-a-method-that-to-be-called-by-views/122315 "2007-12-06T16:07:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/49beb7/32.png) [@sun](https://www.ruby-forum.com/u/sun)\
**Post date:** [December 6, 2007, 4:07pm UTC](https://www.ruby-forum.com/t/how-where-to-define-a-method-that-to-be-called-by-views/122315/1 "2007-12-06T16:07:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/49beb7/32.png) [@sun](https://www.ruby-forum.com/u/sun)\
**Post date:** [December 6, 2007, 4:17pm UTC](https://www.ruby-forum.com/t/how-where-to-define-a-method-that-to-be-called-by-views/122315/2 "2007-12-06T16:17:08Z")

</div>

On 12/6/07, Sun [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> May some one shed some light on this?  
> Yes that’s what helpers are for.

Also, the rails-talk list is a better place to ask questions about  
rails.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![sun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/49beb7/32.png) [@sun](https://www.ruby-forum.com/u/sun)\
**Post date:** [December 6, 2007, 4:26pm UTC](https://www.ruby-forum.com/t/how-where-to-define-a-method-that-to-be-called-by-views/122315/3 "2007-12-06T16:26:30Z")

</div>

On Dec 6, 9:02 am, “Sun” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> May some one shed some light on this?
> 
> Thanks.

You’ll need to post some code. It’s basically done like this:

```auto
app/controllers/my_controller.rb:
class MyController < ActiveController::Base
  def index
    @somevar = 'Hi!'
  end
end

app/views/my_controller/index.rhtml:
<p><%= @somevar %></p>
```

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)

```auto
app/helpers/my_controller_helper.rb:
module MyControllerHelper
  def somevar
    'Hi!'
  end
end
```

now your controller/view looks like this

```auto
app/controllers/my_controller.rb:
class MyController < ActiveController::Base
  def index
  end
end

app/views/my_controller/index.rhtml:
<p><%= somevar %></p>
```

---

<div class="post-metadata">

**Author:** ![sun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/49beb7/32.png) [@sun](https://www.ruby-forum.com/u/sun)\
**Post date:** [December 6, 2007, 4:27pm UTC](https://www.ruby-forum.com/t/how-where-to-define-a-method-that-to-be-called-by-views/122315/4 "2007-12-06T16:27:41Z")

</div>

“Reacher” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

> > answer  
> > def index  
> > app/helpers/my\_controller\_helper.rb:  
> > def index  
> > end  
> > end
> 
> app/views/my\_controller/index.rhtml:
> 
> \<%= somevar %\>
> 
> [/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. .
