在我的application_helper.rb文件中,我有一个像这样的函数:
def find_subdomain
request.domain
end
未定义本地变量或方法 `request'
我在另一个帮助程序中调用此方法。如何在不从控制器传递任何参数的情况下在帮助程序中获取域。
在我的application_helper.rb文件中,我有一个像这样的函数:
def find_subdomain
request.domain
end
未定义本地变量或方法 `request'
我在另一个帮助程序中调用此方法。如何在不从控制器传递任何参数的情况下在帮助程序中获取域。
我知道这已经过时了,但最近我也碰到了同样的问题,所以我想提供一下我的建议。你可以将这个方法添加到你的ApplicationController
中,并将其指定为一个helper_method
:
class ApplicationController < ActionController::Base
helper_method :find_subdomain
private
def find_subdomain
request.domain
end
end
<%= find_subdomain(request) %>
类 ApplicationController < ActionController::Base
helper :all
end
request
? - manu29.dcontroller.request
,但只使用request
应该就可以了。至少在 Rails 4 中是这样的。 - Marcin Adamczykrequest
对象作为方法参数传递。 - Arup Rakshit