在我的 application_helper.rb 文件中,我有一个这样的函数:
def find_subdomain
request.domain
end
undefined local variable or method `request'
我正在另一个助手中调用这个方法。我如何在不从 Controller 传递任何参数的情况下获取助手中的域。
最佳答案
我知道这是旧的,但最近我自己偶然发现了这个,我想我应该参与其中。您可以将该方法添加到您的 ApplicationController
并将其指定为 helper_method
改为:
class ApplicationController < ActionController::Base
helper_method :find_subdomain
private
def find_subdomain
request.domain
end
end
关于ruby-on-rails - 在 Rails 助手中访问请求对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23954410/