# How to explore the environment?

**URL:** <https://www.ruby-forum.com/t/how-to-explore-the-environment/180957>\
**Category:** Rails\
**Created:** [January 2, 2010, 12:38am UTC](https://www.ruby-forum.com/t/how-to-explore-the-environment/180957 "2010-01-02T00:38:42Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [January 2, 2010, 12:38am UTC](https://www.ruby-forum.com/t/how-to-explore-the-environment/180957/1 "2010-01-02T00:38:42Z")

</div>

It’s my first rails project and something is broken: my action  
controllers can’t access methods defined in the helper modules.

The only thing, I successfully verified (by setting a debugger statement  
at the very beginning of the helper modules) is, that the helper modules  
get loaded before the action controller is called.

None the less, the methods defined in the helpers, are unknown to the  
action controllers.

I can fix the problem by duplicating the helper methods in the  
controller module, but that’s a hack.

Any idea, how to find the reason?

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [January 2, 2010, 1:03am UTC](https://www.ruby-forum.com/t/how-to-explore-the-environment/180957/2 "2010-01-02T01:03:05Z")

</div>

On Jan 1, 6:38 pm, Fritz T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I can fix the problem by duplicating the helper methods in the  
> controller module, but that’s a hack.
> 
> ## Any idea, how to find the reason?
> 
> Posted viahttp://www.ruby-forum.com/.

[http://api.rubyonrails.org/classes/ActionController/Helpers/ClassMethods.html#M000490](http://api.rubyonrails.org/classes/ActionController/Helpers/ClassMethods.html#M000490)  
(the documentation sort-of lies, as the helper() method does not  
actually “include” the specified module)

helper WhateverHelper will give the view access to the helper module’s  
instance methods  
include WheteverHelper will, being plain-old-Ruby, include the  
WhateverHelper module into the controller

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [January 2, 2010, 1:28am UTC](https://www.ruby-forum.com/t/how-to-explore-the-environment/180957/3 "2010-01-02T01:28:51Z")

</div>

Fritz T. wrote:

> ```
> It's my first rails project and something is broken: my 
> action
> controllers can't access methods defined in the helper modules.
> 
> The only thing, I successfully verified (by setting a debugger statement  
> at the very beginning of the helper modules) is, that the helper modules  
> get loaded before the action controller is called.
> 
> None the less, the methods defined in the helpers, are unknown to the  
> action controllers.
> 
> I can fix the problem by duplicating the helper methods in the  
> controller module, but that’s a hack.
> 
> Any idea, how to find the reason?
> ```

The helpers are more accurately called view helper methods.&nbsp; They are not intended for use in the controllers.  

--

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.

To post to this group, send email to  
removed\_email\_address@domain.invalid.

To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.

For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en.](http://groups.google.com/group/rubyonrails-talk?hl=en.)

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [January 2, 2010, 2:32am UTC](https://www.ruby-forum.com/t/how-to-explore-the-environment/180957/4 "2010-01-02T02:32:39Z")

</div>

If you want to use the same methods in both environments (view and  
controller), you can do like this (in controller):

def method\_a

end

def method\_b

end

helper\_method :method\_a, :method\_b

Then the methods will be available also in views.

2010/1/1 Norm S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> ## action controllers.
> 
> You received this message because you are subscribed to the Google G.  
> “Ruby on Rails: Talk” group.  
> To post to this group, send email to removed\_email\_address@domain.invalid.  
> To unsubscribe from this group, send email to  
> removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> .  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
Bruno Grasselli  
Blog: [http://brunograsselli.com.br](http://brunograsselli.com.br)  
Twitter: [http://twitter.com/grasselli](http://twitter.com/grasselli)

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [January 2, 2010, 2:37pm UTC](https://www.ruby-forum.com/t/how-to-explore-the-environment/180957/5 "2010-01-02T14:37:00Z")

</div>

Thanks for your replies.

Now I read the API reference of ActionController::Helpers::ClassMethods  
and took a look into my ApplicationController, which has been generated  
via RadRails:

class ApplicationController \< ActionController::Base  
helper :all # include all helpers, all the time

protect\_from\_forgery  
end

If I understand the doc, helper :all should do the job for all  
controllers.

To verify this, I set a debugger call as first statement in my crashing  
controller and give

y ActionController::Base.helpers\_dir

into the debugger. The response is:

NoMethodError Exception: undefined method `helpers\_dir’ for  
ActionController::Base:Class

So what is going wrong there?

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [January 2, 2010, 3:54pm UTC](https://www.ruby-forum.com/t/how-to-explore-the-environment/180957/6 "2010-01-02T15:54:26Z")

</div>

On Jan 2, 8:37 am, Fritz T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> NoMethodError Exception: undefined method `helpers\_dir’ for  
> ActionController::Base:Class
> 
> ## So what is going wrong there?
> 
> Posted viahttp://www.ruby-forum.com/.

As I said: ActionController::Base.helper does not actually include  
helper modules into the controller; it makes them accessible in the  
view. You’ll need to

include ApplicationHelper # or whatever appropriate module

to have the methods accessible in the controller.

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [January 2, 2010, 4:03pm UTC](https://www.ruby-forum.com/t/how-to-explore-the-environment/180957/7 "2010-01-02T16:03:17Z")

</div>

pharrington wrote:

> As I said: ActionController::Base.helper does not actually include  
> helper modules into the controller; it makes them accessible in the  
> view. You’ll need to
> 
> include ApplicationHelper # or whatever appropriate module
> 
> to have the methods accessible in the controller.

In deed, that helps. Sorry for my ignorance.

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [January 2, 2010, 4:15pm UTC](https://www.ruby-forum.com/t/how-to-explore-the-environment/180957/8 "2010-01-02T16:15:05Z")

</div>

But there is still one annoying point:

My controllers worked using methods from ApplicationHelper until  
yesterday morning, when I tried to modify routes.rb and run sume rake  
jobs from RadRails that I didn’t really understand, including rake  
rails:update

The changes in routes.rb have been rolled back, but that didn’t help

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [January 2, 2010, 4:24pm UTC](https://www.ruby-forum.com/t/how-to-explore-the-environment/180957/9 "2010-01-02T16:24:52Z")

</div>

On Jan 2, 10:15 am, Fritz T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> rake rails:update

Then perhaps helper() actually included the helper modules into the  
controller in an earlier version of Rails. Its hard for me to remember  
at this point, but I know since at least 2.2 helper()'s behaved as it  
does now.

Just curious now; what version of Rails did you have previously  
installed, and what version did you upgrade to before running rake  
rails:update?

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [January 2, 2010, 5:19pm UTC](https://www.ruby-forum.com/t/how-to-explore-the-environment/180957/10 "2010-01-02T17:19:58Z")

</div>

One more issue.

class ZaehlersController \< ApplicationController  
def index  
if keineZaehlerVorhanden  
blah  
end

```
respond_to do |format|
  format.html # index.html.erb
  format.xml { render :xml => @zaehlers }
end

```

end

if !keineZaehlerVorhanden  
verify :session =\> :user\_name,  
:add\_flash =\> {:error =\> “error”},  
:redirect\_to =\> {:controller =\> “authentication”, :action =\>  
“login”}  
end  
end

If it gets called,

if !keineZaehlerVorhanden  
veriy…  
end

fails with "NameError (undefined local variable or method  
`keineZaehlerVorhanden’ for ZaehlersController:Class):

After removing the condition around the verify, the index action  
terminates successfully - including the

if keineZaehlerVorhanden  
blah  
end

Why this?

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [January 2, 2010, 5:02pm UTC](https://www.ruby-forum.com/t/how-to-explore-the-environment/180957/11 "2010-01-02T17:02:45Z")

</div>

pharrington wrote:

> Just curious now; what version of Rails did you have previously  
> installed, and what version did you upgrade to before running rake  
> rails:update?

I initially installed ruby and rails using synaptic on Ubuntu 9.04. It  
was ruby 1.8 and rails 2.1.0-6.

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [January 2, 2010, 5:22pm UTC](https://www.ruby-forum.com/t/how-to-explore-the-environment/180957/12 "2010-01-02T17:22:59Z")

</div>

I forgot to say: keineZaehlerVorhanden is defined in ApplicationHelper,  
which is included in ApplicationController.

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [January 2, 2010, 6:12pm UTC](https://www.ruby-forum.com/t/how-to-explore-the-environment/180957/13 "2010-01-02T18:12:34Z")

</div>

On Jan 2, 11:19 am, Fritz T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> format.xml { render :xml => @zaehlers }
> end
> 
> ```
> 
> end

Look at this carefully. You’re closing the respond\_to block, and then  
closing the index method. keineZaehlerVorhanden isn’t a class method,  
so of course the error will be thrown.

the error message "fails with “NameError (undefined local variable or  
method  
`keineZaehlerVorhanden’ for **ZaehlersController:Class** )” gives a  
prettttttttyyy good clue as to whats going on.

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [January 2, 2010, 6:32pm UTC](https://www.ruby-forum.com/t/how-to-explore-the-environment/180957/14 "2010-01-02T18:32:33Z")

</div>

pharrington wrote:

> On Jan 2, 11:19ï¿½am, Fritz T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ï¿½ ï¿½ ï¿½ format.xml ï¿½{ render :xml =\> @zaehlers }  
> > ï¿½ ï¿½ end  
> > ï¿½ end
> 
> Look at this carefully. You’re closing the respond\_to block, and then  
> closing the index method. keineZaehlerVorhanden isn’t a class method,  
> so of course the error will be thrown.

keineZaehlerVorhanden is a method of ApplicationController and it works  
inside the index action.

The verify statement is written usually at class level. Right? My idea  
was, only to verfy, if the method keineZaehlerVorhanden fails.

The interesting story is, that the construct around the verify worked  
fine until I accidentally run a rake rails:update

Seems, that the updated rails version changed something in the handling  
of controllers.

My question: How to implement a verification, that should not be  
executed under a certain condition?

(Background:  
The action should be executable for anybody, as long as no users are  
added to the data base - aka installation phase of my app.)

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [January 2, 2010, 6:49pm UTC](https://www.ruby-forum.com/t/how-to-explore-the-environment/180957/15 "2010-01-02T18:49:26Z")

</div>

On Jan 2, 12:32 pm, Fritz T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> keineZaehlerVorhanden is a method of ApplicationController and it works
> 
> My question: How to implement a verification, that should not be  
> executed under a certain condition?

Well now, verify only exists to prevent actions from even being  
_invoked_ unless certain conditions of the request are met. If you  
need tighter control, to filter based on more than just the request,  
just use a before\_filter. Either way, you should probably focus on  
_precisely_ what you’re trying to accomplish, and the solution will  
probably be obvious (and definitely simpler than doing the same check  
in two different places).

> ## (Background: The action should be executable for anybody, as long as no users are added to the data base - aka installation phase of my app.)
> 
> Posted viahttp://www.ruby-forum.com/.

OK yeah, this is before\_filter material. Don’t know if the lack of  
users in the DB appropriately denotes the “installation phase,” but  
thats for you to think about.

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![trapper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/2acd7d/32.png) [@trapper](https://www.ruby-forum.com/u/trapper)\
**Post date:** [January 2, 2010, 7:17pm UTC](https://www.ruby-forum.com/t/how-to-explore-the-environment/180957/16 "2010-01-02T19:17:26Z")

</div>

Thanks, now it works as expected.
