# Strange thing with controller's helper method and method return value

**URL:** <https://www.ruby-forum.com/t/strange-thing-with-controllers-helper-method-and-method-return-value/129644>\
**Category:** Rails\
**Created:** [February 20, 2008, 8:54am UTC](https://www.ruby-forum.com/t/strange-thing-with-controllers-helper-method-and-method-return-value/129644 "2008-02-20T08:54:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hei](https://www.ruby-forum.com/u/hei)\
**Post date:** [February 20, 2008, 8:54am UTC](https://www.ruby-forum.com/t/strange-thing-with-controllers-helper-method-and-method-return-value/129644/1 "2008-02-20T08:54:49Z")

</div>

1, why my helper method does not work  
I hava a controller named msg, and i define a method test(id) in /  
app/helpers/msg\_helper.rb  
but i can not use this method in /app/controllers/  
msg\_controller.rb.  
Can anyone tells me why ?Doedn’t rails require msg\_helper.rb for  
me ?  
2, how can i use a hash type variable returned by a method  
Because i can’t use helper method, i write the method just in  
msg\_controller.rb  
like:  
:private  
def test(id)  
xx = {}  
xx[:test] = 1  
return xx  
end  
and i find xx i get via xx = test(id) is nil  
I think in ruby method hash type variable will be returned by  
value(or maybe reference, but the variable will not out of scope)  
but why can i get the true value ?  
Thanks very much~~  
My enviroment:  
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]  
Rails 2.0.2  
oracle 10

---

<div class="post-metadata">

**Author:** ![hei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hei](https://www.ruby-forum.com/u/hei)\
**Post date:** [February 20, 2008, 9:32am UTC](https://www.ruby-forum.com/t/strange-thing-with-controllers-helper-method-and-method-return-value/129644/2 "2008-02-20T09:32:55Z")

</div>

> 1, why my helper method does not work  
> I hava a controller named msg, and i define a method test(id) in /  
> app/helpers/msg\_helper.rb  
> but i can not use this method in /app/controllers/  
> msg\_controller.rb.  
> Can anyone tells me why ?Doedn’t rails require msg\_helper.rb for  
> me ?

A helper is simply a module containing methods that assist a view not  
your controller. If you want make method for your controller and tobe  
used to many controller you can put your method in application.rb

> and i find xx i get via xx = test(id) is nil

Sorry I not understand what do you mean on sentences above.

~_~_~_~_~_~_~_~_~_~_~_~_~_~_~_~_~\*  
Reinhart Ariando  
YM : booking2heaven  
Web: [http://teapoci.blogspot.com](http://teapoci.blogspot.com)  
~_~_~_~_~_~_~_~_~_~_~_~_~_~_~_~_~\*

---

<div class="post-metadata">

**Author:** ![hei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hei](https://www.ruby-forum.com/u/hei)\
**Post date:** [February 20, 2008, 1:52pm UTC](https://www.ruby-forum.com/t/strange-thing-with-controllers-helper-method-and-method-return-value/129644/3 "2008-02-20T13:52:13Z")

</div>

On 20 Feb 2008, at 12:41, hei wrote:

> > > msg\_controller.rb.  
> > > developer
> 
> 1. Look in the directory app/models for a model in the file store.rb  
> and  
> load it if found.  
> That’s just wrong - it’s never been like that.

> Add this to one of your controller  
> def test\_action  
> new = {}  
> value = test\_method  
> new.merge(value)  
> merge doesn’t alter the hash, it returns a new hash with the merge  
> contents (merge! does an inplace merge)

Fred

---

<div class="post-metadata">

**Author:** ![hei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hei](https://www.ruby-forum.com/u/hei)\
**Post date:** [February 20, 2008, 1:42pm UTC](https://www.ruby-forum.com/t/strange-thing-with-controllers-helper-method-and-method-return-value/129644/4 "2008-02-20T13:42:48Z")

</div>

On Feb 20, 4:32 pm, Visit Indonesia 2008 \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> used to many controller you can put your method in application.rb

See a content I copy from “Agile Web D. with Rails 2nd” 18.1  
Context and Dependencis  
Rails handles many configuration dependencies automatically; as a  
developer  
you can normally rely on it to do the right thing. For example, if a  
request arrives for [http://my.url/store/list](http://my.url/store/list), Rails will do the  
following.

1. Load the file store\_controller.rb in the directory app/  
controllers. (This  
loading takes place only once in a production environment).
2. Instantiate an object of class StoreController.  
@@@ 3. Look in app/helpers for a file called store\_helper.rb. If  
found, it is loaded  
and the module StoreHelper is mixed into the controller object.
3. Look in the directory app/models for a model in the file store.rb  
and  
load it if found.  
Do you see 3 ? is rails 2.0.2 changes this ?

> > and i find xx i get via xx = test(id) is nil
> 
> Sorry I not understand what do you mean on sentences above.

I mean , test(id) is supposed to renturn a hash with value {:text =\>  
1}  
but actually , it returns nil or {}  
You can have a simple test  
Add this to one of your controller  
def test\_action  
new = {}  
value = test\_method  
new.merge(value)  
render :text =\> new[:text]  
end  
:private  
def test\_method()  
value = {:text =\> 1}  
return value  
end  
Then visit [http://root/controller/test\_action](http://root/controller/test_action) to see what it returns  
Actually, ‘value’ returned by test\_method is something like

flash: !map:ActionController::Flash::FlashHash  
:value: &id001  
:text: 1  
:value: \*id001  
I think new.merge(value) makes this  
Can anyone tells me more details ?  
Thanks!

---

<div class="post-metadata">

**Author:** ![hei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hei](https://www.ruby-forum.com/u/hei)\
**Post date:** [February 20, 2008, 1:57pm UTC](https://www.ruby-forum.com/t/strange-thing-with-controllers-helper-method-and-method-return-value/129644/5 "2008-02-20T13:57:33Z")

</div>

emm…  
Just basic knowledges…  
I almost spent two hours on it …  
Thank you very much~~~

On Feb 20, 8:51 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
