简体   繁体   English

Rails:request.subdomains在应用程序控制器中为请求引发undef方法

[英]Rails: request.subdomains raises undef method for request when in application controller

I am trying to extract the subdomain in the application controller, for app wide use, this way, 我试图在应用程序控制器中提取子域,以供应用程序广泛使用,这样,

@subdomain = request.subdomains(0)

While this code works in any other controller, in the app controller it throws an 虽然此代码适用于任何其他控制器,但在app控制器中它会抛出一个

undefined local variable or method `request' for ApplicationController:Class

exception. 例外。

I am running rails 3.2.2 on Lion. 我在Lion上运行rails 3.2.2。

I borrowed it from DHH's code fragment for basecamp style subdomains - 我从DHH的basecamp样式子域代码片段中借用了它 -

class ApplicationController < ActionController::Base
  before_filter :set_current_account
  private
    def set_current_account
      @current_account = Account.find_by_subdomain!(request.subdomains.first)
    end
end

What am I missing? 我错过了什么?

When you're getting that error, it's happenning becaue you're calling request inside ApplicationController 's class instead of inside a method. 当你收到这个错误时,就会发生这种情况,因为你在ApplicationController的类中而不是在方法内部调用request Are you sure that the code you've shown us is correct? 您确定您向我们展示的代码是否正确?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM