2

My application_controller.rb has:

helper :all

my application_helper.rb has:

def authorized?
   false
end

Now in my:

Admin::PostsController < ApplicationController

  before_filter :authorized?

I get the error:

undefined method `authorized?'

This makes no sense to me, helper :all is set, and this controller inherits from the application_controller.

What am I missing?

Blankman
  • 259,732
  • 324
  • 769
  • 1,199

3 Answers3

4

Answer to a similar question is here

UPDATE

the answer there was as follows

helper :all makes all the helpers (yes, all of them) available in the views, it does not include them into the controller.

If you wish to share some code between helper and controller, which is not very desirable because helper is UI code and controller is, well, controller code. You can either include the helper in the controller or create separate module and include that in the controller and the helper also.

Community
  • 1
  • 1
Srushti
  • 314
  • 2
  • 8
1

You need to move your authorized method into application_controller. When you do helper :all in application_controller, you are basically making all of your application controller methods accessible in helper, not vice versa. What you are looking for is to be able to define the method in the helper and have it accessible in controllers. Instead, just move the method from the helper to application controller and it should work.

Also, if you still want to be able to access the helper method in the controllers you need to do @template.authorized?. Not sure if that works in rails 3.

Chirantan
  • 15,304
  • 8
  • 49
  • 75
0

I don't know. But include ApplicationHelper should serve as a workaround.

Paul Schreiber
  • 12,531
  • 4
  • 41
  • 63