Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-pluginsrenderpartial

How to render a partial in a plugin from a plugin helper


I have a helper in my plugin test_plugin/lib/test_helper.rb:

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

I have a partial test_plugin/app/views/test_plugin/_test_partial.html.erb:

<p>Hello World!</p>

In my application app/views/tests/index.html.erb when I do this:

<%= test_render %>

I get the following error:

Missing partial test_plugin/test_partial with {:locale=>[:en, :en], :formats=>[:html], :handlers=>[:rjs, :rhtml, :haml, :builder, :rxml, :erb]} in view paths "/home/####/workspace/my_application/app/views"


Solution

  • The following works for me on Rails 2.3.5 (ie: it searches for this partial in vendor/plugins/test_plugin/app/views/test_plugin):

    vendor/plugins/test_plugin/init.rb

    require 'test_plugin'
    

    vendor/plugins/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
    

    vendor/plugins/test_plugin/lib/test_plugin_helpers.rb

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

    vendor/plugins/test_plugin/app/views/test_plugin/_test_partial.html.erb

    Yuppie!
    

    app/views/test.html.erb

    <%= test_render %>
    

    Something similar should work also in Rails3.