How to explore the environment?

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?

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

Fritz Trapper 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. They are not intended
for use in the controllers.

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.

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?

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.

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.

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

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?

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.

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?

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

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.

pharrington 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.)

pharrington 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?

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.

Thanks, now it works as expected.