【问题标题】:How to render a partial in a plugin from a plugin helper如何从插件助手渲染插件中的部分内容
【发布时间】:2010-10-22 20:48:31
【问题描述】:

我的插件中有一个助手test_plugin/lib/test_helper.rb

module TestHelper
  def test_render
    render 'test_plugin/test_partial'
  end
end

我有一个部分test_plugin/app/views/test_plugin/_test_partial.html.erb

<p>Hello World!</p>

当我这样做时,在我的应用程序 app/views/tests/index.html.erb 中:

<%= test_render %>

我收到以下错误:

缺少部分 test_plugin/test_partial 与 {:locale=>[:en, :en], :formats=>[:html], :handlers=>[:rjs, :rhtml, :haml, :builder, :rxml, :erb]} 在视图路径中 "/home/####/workspace/my_application/app/views"

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-plugins render partial


    【解决方案1】:

    以下内容适用于 Rails 2.3.5(即:它在 vendor/plugins/test_plugin/app/views/test_plugin 中搜索此部分内容):

    供应商/插件/test_plugin/init.rb

    require 'test_plugin'
    

    供应商/插件/test_plugin/lib/test_plugin.rb

    require 'test_plugin_helpers'
    
    # Helpers will be available in all controllers
    ActionController::Base.send :include, TestPlugin::Helpers
    
    # Helpers will be available in the views
    ActionView::Base.send :include, TestPlugin::Helpers
    

    供应商/插件/test_plugin/lib/test_plugin_helpers.rb

    module TestPlugin
      module Helpers
        def test_render
          render 'test_plugin/test_partial'
        end
      end
    end
    

    供应商/插件/test_plugin/app/views/test_plugin/_test_partial.html.erb

    Yuppie!
    

    app/views/test.html.erb

    <%= test_render %>
    

    类似的东西应该也可以在 Rails3 中使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-20
      • 1970-01-01
      • 1970-01-01
      • 2012-01-31
      • 1970-01-01
      相关资源
      最近更新 更多