如何在Ruby on Rails中从RJS助手访问视图助手?
问题描述:
我有一些RJS的常见位,我在多个RJS文件中重复,因此想将它放入帮助程序中。如何在Ruby on Rails中从RJS助手访问视图助手?
我为app/helpers/application_helper.rb添加了一个方法,就像我通常为html.erb文件一样。该方法的工作原理,除了它不能调用视图助手的RJS文件可以。
之一,在RJS文件工作的线路是:
notices = flash.collect { |type,msg| content_tag(:p, msg, :class => type) }
但content_tag是不是从RJS帮手访问。为什么不,我怎么从那里访问它?
答
这样的事情呢?
#application_helper.rb
def view_helpers
ActionController::Base.helpers
end
def do_it
view_helpers.content_tag :div, :class => 'yes'
end
你有你RJS太调用任何方法,这是不是很好用view_helpers
,但我想它在模型(仅在2.3和3.1获得项目)和工作...