基本上我有一个 UsersInitializeController 类
class UsersInitializeController < ApplicationController
before_filter :authenticate_user!
def create
render true
end
end
验证用户!在应用程序 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
当我的应用程序启动时,它向 UsersInitializeController 发送 POST 请求。由于设置了 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
### 问题已解决###
未使用的 Devise Gem 不知何故导致了并发症。删除它并完成。
最佳答案
在“包含的 do” block 中添加 before_filter:
included do
before_filter :authenticate_user!
end
更新: 刚注意到你已经解决了。但是我遇到了同样的麻烦,上面的解决方案解决了我的问题。所以,我会在这里留下评论,因为它可能会帮助其他人
关于ruby-on-rails - ActionController::RoutingError(类的未定义方法 `before_filter'):错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9579865/