2012-03-06 26 views
2

基本上我有一个UsersInitializeController类的ActionController :: RoutingError(未定义的方法`的before_filter”类):错误

class UsersInitializeController < ApplicationController 
    before_filter :authenticate_user! 

    def create 
    render true 
    end 
end 

的authenticate_user!在应用程序控制器中找到

class ApplicationController < ActionController::Base 
    # protect_from_forgery 

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

end 

当我的应用程序启动时,它将POST请求发送到UsersInitializeController。由于before_filter已设置,因此它将调用authenticate_user!第一。然而,我得到的错误说,before_filter是一个未定义的方法。

据我所知,before_filter存在于ActionController中,并且自UsersInitializeContoller < ApplicationController < ActionController,我不应该得到这个错误。有没有人遇到过这个问题?

异常堆栈(如需要)

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文件(如需要)

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

###的问题解决了###

未使用的设计宝石以某种方式导致并发症。删除它并完成。

+1

你可以发布整个异常堆栈吗?看看你在标题中使用的错误,我认为问题不在于你的authenticate_user方法。这是一个路由错误,未定义的方法是before_filter。 – 2012-03-06 08:12:40

+0

我已按要求编辑! – 2012-03-06 08:36:27

+1

很奇怪。它建议在加载Rails之前调用before_filter方法。如果将before_filter移动到应用程序控制器并执行相同的操作,会发生什么情况? – 2012-03-06 09:54:20

回答

0

无法重现,您发布的代码在我的Rails 3.2.2应用程序中工作正常。

可能是你的源文件有问题(即某些额外的隐藏字节在某处)。

你可以试着一步一步的方法来解决这个问题:

  1. 添加新UsersController并添加resources :usersroutes.rb
  2. 用下面的代码添加index动作:

    def index 
        render :text => "Hello there" 
    end 
    
  3. 当您访问http://localhost:3000您应该看到文字“你好”

  4. 添加before_filter并验证过滤器是通过添加例如logger.warn('In the Filter')的过滤方法
+0

有用的文字,但更好地作为评论。 “我无法重现你的问题”不是一个答案。 – 2012-08-13 20:57:45

1

的开头添加的before_filter内的“包括做”块:

included do 
    before_filter :authenticate_user! 
end 

更新: 只注意到你解决它了。然而,我遇到了同样的麻烦,上面的解决方案解决了我的情况。所以,我会在这里留下评论,因为它可以帮助其他人

相关问题