2
votes

Basically I have a UsersInitializeController Class

class UsersInitializeController < ApplicationController
  before_filter :authenticate_user!

  def create
    render true
  end
end

authenticate_user! is found in the Application Controller

class ApplicationController < ActionController::Base
  # protect_from_forgery

  def authenticate_user!
    @current_user = User.find_by_token params[:auth_token]
    if !@current_user
      @current_user = User.create :token => params[:auth_token]
    end
  end

end

When my application starts, it sends POST request to the UsersInitializeController. Since before_filter is set, it will thus call authenticate_user! first. However the error I got says before_filter is an undefined method.

From my knowledge, before_filter exist in ActionController, and since UsersInitializeContoller < ApplicationController < ActionController, I shouldn't be getting this error. Has anyone encounter this issue before ?

Exception Stack (as requested)

Started POST "/users_initialize.json" for 127.0.0.1 at 2012-03-06 00:32:50 -0800

ActionController::RoutingError (undefined method `before_filter' for UsersInitializeController:Class):
app/controllers/users_initialize_controller.rb:3:in `<class:UsersInitializeController>'
app/controllers/users_initialize_controller.rb:1:in `<top (required)>'

Routes.rb file (as requested)

MyApplication::Application.routes.draw do
 resources :users_initialize
 match 'info/required_client_version' => 'info#required_client_version'
end

### Problem Solved ###

Unused Devise Gem somehow causing the complication. Removed it and done.

2
Can you post the entire exception stack. Looking at the error you've used in the title, I don't think the issue is with your authenticate_user method. It's a Routing Error and the undefined method is before_filter. - Robin Fisher
Very odd. It suggests that the before_filter method is being called before Rails has loaded. What happens if you move the before_filter to your Application Controller and perform the same action? - Robin Fisher
Also, what is the name of the related helper file and what class does it define? - Robin Fisher
Please post the contents of your routes.rb file as well. - Wolfgang
@RobinFisher I encounter the same problem even if I move before_filter to Application Controller. Not sure i understand your question - John Lee

2 Answers

1
votes

add the before_filter within an "included do" block:

included do
    before_filter :authenticate_user!
end

Update: just noticed you solved it already. However I was having the same troubles and the solution above solved it in my case. So, I'll leave the comment here since it may help others

0
votes

Not able to reproduce, the code you posted works fine on my Rails 3.2.2 app.

Probably there is something wrong with your source file (i.e. some extra hidden bytes somewhere).

You could try a step-by-step approach to solve this:

  1. add a new UsersController and add resources :users to routes.rb
  2. add an index action with the following code:

    def index 
        render :text => "Hello there"
    end
    
  3. When you visit http://localhost:3000 you should see the text "Hello there"

  4. Add the before_filter and verify that the filter is executed by adding e.g. logger.warn( 'In the Filter' ) to the beginning of the filter method