【发布时间】:2012-12-06 17:03:47
【问题描述】:
我有一个 helper_method,它允许链接从子域中转义。但是它正在影响我的videos_controller,因为当它不在事件控制器中时,它基本上似乎否定了'current_event'方法。
在过去的 4 天里,我尝试了几十种不同的方法来实现它,因此我仍然可以从子域中逃脱我的链接,但仍然允许 videos_controller 工作。
我认为实现这一目标的最佳方法是从辅助方法中排除 videos_controller,但我不确定如何(或者它是否实际上是最好的前进方式 - 我显然是一个菜鸟!)任何建议请?!相关代码如下:
module UrlHelper
def url_for(options = nil)
if request.subdomain.present? and request.subdomain.downcase != 'www' and !options.nil? and options.is_a?(Hash) and options.has_key? :only_path and options[:only_path]
options[:only_path] = false
end
super
end
end
视频控制器
def new
if current_event?
@video = current_event.videos.new
else
@video = Video.new
end
end
def create
if current_event.present?
@video = current_event.videos.new(params[:video])
@video.user_id = current_user.id
key = get_key_from_the_cloud
@video.key = key
else
@video = current_user.videos.new(params[:video])
@video.user_id = current_user.id
key = get_key_from_the_cloud
@video.key = key
end
if @video.save
flash[:success] = "Video uploaded!"
redirect_to root_url(subdomain: => current_event.name)
else
flash[:error] = "#{@video.errors.messages}"
render :new
end
end
current_event 方法
def current_event
if request.subdomain.present?
@event = Event.find_by_name(request.subdomain)
end
end
【问题讨论】:
-
你看过this post了吗?
-
我没有,但我现在已经尝试过了,但它并没有帮助解决问题。出于某种原因,我不能只在我的事件控制器中包含 urlhelper(而且我很确定我正在尝试正确地在“EventsController”下“包含 UrlHelper”
-
您是否尝试过包含是否有效?您可能想创建一个新函数
def test,它只执行puts "Test is called"之类的功能,如果可行,您知道它不包括失败,但它必须是方法。否则,您知道该模块不包括在内,您可以缩小搜索范围。 -
我已经确定它不包括在内。仅包含我的“应用程序助手”,因为当我将测试方法放入其中时,测试方法有效,但当我将其放入任何其他帮助方法并尝试将其包含在事件控制器中时,测试方法无效。
-
谢谢!我已经重新检查了我是如何包含辅助方法的,但它毕竟是错误的,所以它现在只能在该控制器上本地工作。关于 SO 最佳实践的快速 q - 我现在应该删除这个问题还是离开它?再次感谢。
标签: ruby-on-rails-3 subdomain helpermethods