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.
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
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.
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
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
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
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?
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.
� � � 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.