ActionController :: RoutingError(类的未定义方法`before_filter'):错误

时间:2012-03-06 08:00:05

标签: ruby-on-rails ruby ruby-on-rails-3

基本上我有一个UsersInitializeController类

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 !@current_user
      @current_user = User.create :token => params[:auth_token]
    end
  end

end

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

据我所知,before_filter存在于ActionController中,并且自UsersInitializeContoller&lt; ApplicationController&lt; 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

###问题已解决###

未使用的Devise Gem不知何故导致并发症。删除它并完成。

2 个答案:

答案 0 :(得分:1)

在&#34;包含do&#34;中添加before_filter。块:

included do
    before_filter :authenticate_user!
end

更新: 刚刚注意到你已经解决了但是我遇到了同样的麻烦,上面的解决方案解决了我的问题。所以,我会在这里留下评论,因为它可以帮助其他人

答案 1 :(得分:0)

无法重现,您发布的代码在我的Rails 3.2.2应用程序上运行正常。

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

您可以尝试逐步解决此问题:

  1. 添加新UsersController并将resources :users添加到routes.rb
  2. 使用以下代码添加index操作:

    def index 
        render :text => "Hello there"
    end
    
  3. 当您访问http://localhost:3000时,您会看到“Hello there”文字

  4. 添加before_filter并通过添加例如来验证过滤器是否已执行logger.warn( 'In the Filter' )到过滤方法的开头
相关问题