【问题标题】:How to use Padrino helpers in a custom helper class如何在自定义助手类中使用 Padrino 助手
【发布时间】:2013-01-22 21:58:06
【问题描述】:

我想使用自定义通知助手在我的 Padrino 应用程序中生成通知(闪存)消息。在助手内部,我想使用内置的 Padrino 助手(flash 和 content_tag。(参见下面的示例)

class NotificationHelper       

  def self.notify
    unless flash.empty?
      content_tag :div, class: 'notifications' do
        flash.map do |key, msg|
          headline = case key
          when :success then 'Super!'
          when :error   then 'Oh. Das tut uns leid.'
          end
          content_tag :p, :class => "notification" do
            content_tag(:span, headline, class: "headline #{key}") + msg
          end
        end.join("\n")
      end
    end
  end
end

但如果我在视图中使用帮助程序,则会出现以下错误: “NoMethodError - NotificationHelper:Class 的未定义方法‘content_tag’:”

我做错了什么?

【问题讨论】:

    标签: padrino


    【解决方案1】:

    也许你应该在助手之后正确注册它:

    module Flash
      def notify # without self
      end
    end
    
    class MyApp < Padrino::Application
      # ...
      # after helpers
      helpers Flash
    end
    

    看看:https://github.com/padrino/padrino-contrib/blob/master/lib/padrino-contrib/helpers/flash.rb

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-07
      • 2020-04-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多