Tag: 辅助

Rails引擎中的助手

我正在使用rails引擎,我对帮助器有问题。 显然这是一个已知的“问题”,但那里没有很多解决方案。 问题是我有一个我想要全局访问的AuthenticationHelper – 但它不起作用。 我已经读过你可以在你的init.rb添加几行但它似乎没有任何效果。 知道什么是在引擎中提供应用程序的最佳方法吗? 编辑:修正了 – 只需将代码(来自链接)放入engine.rb 。

Rails引擎:仅在重新启动服务器时重新加载助手

我目前正在尝试将function转移到引擎中。 到目前为止它运行良好,但我有点困惑为什么引擎的某些部分总是在发生变化时自动重新加载,而有些则不然。 具体来说,在添加辅助方法时,我必须重新启动Rails服务器,否则Rails不会看到它。 这是正常的行为吗? 这是我的引擎的相关部分: 组件/ iq_list.rb # encoding: utf-8 require ‘iq_list/engine’ # Load IqList Modules module IqList extend ActiveSupport::Autoload autoload :Helpers autoload :Models autoload :Controllers end 组件/ iq_list / engine.rb module IqList class Engine < ::Rails::Engine end end 组件/ iq_list / helpers.rb module IqList module Helpers extend ActiveSupport::Autoload autoload :IqListHelper end end 组件/ iq_list […]

在helper中的content_tag内循环并输出content_tags

我正在尝试一个帮助方法,它将输出一个项目列表,如下所示: foo_list( [‘item_one’, link_to( ‘item_two’, ‘#’ ) … ] ) 我在阅读了使用rails 3中的helper输出html之后,就像这样编写了帮助器: def foo_list items content_tag :ul do items.collect {|item| content_tag(:li, item)} end end 但是,如果我这样做,我只是在这种情况下得到一个空的UL: def foo_list items content_tag :ul do content_tag(:li, ‘foo’) end end 我按预期获得了UL和LI。 我已经尝试过将它交换一下: def foo_list items contents = items.map {|item| content_tag(:li, item)} content_tag( :ul, contents ) end 在这种情况下,我得到整个列表,但LI标签是html转义(即使字符串是HTML安全)。 做content_tag(:ul, contents.join(“\n”).html_safe )有效,但我感觉不对,我觉得content_tag应该以块模式工作,并以某种方式收集。