rails helper中的访问请求对象

时间:2014-05-30 12:15:59

标签: ruby-on-rails ruby

在我的application_helper.rb文件中,我有一个这样的函数:

def find_subdomain
  request.domain
end
undefined local variable or method `request' 

我在另一个助手中调用此方法。如何在不通过控制器传递任何参数的情况下在helper中获取域。

3 个答案:

答案 0 :(得分:3)

正如其他人提到的那样,请求对象应该传递给你的帮助器,这样你就可以从视图(ERB)传递它,如下所示,

<%= find_subdomain(request) %>

答案 1 :(得分:3)

我知道这已经过时了,但我最近偶然发现了这个问题,我认为我已经填上了。您可以将方法添加到ApplicationController并将其指定为helper_method代替:

class ApplicationController < ActionController::Base
  helper_method :find_subdomain

  private

  def find_subdomain
    request.domain
  end
end

答案 2 :(得分:0)

ApplicationController类

helper:all

结束