Search code examples
rubyruby-on-rails-plugins

How do I publish vendor/_____/public/* files?


I have installed a plugin to my Rails app. The plugin has files in its public directory that I want to make available.

For example, vendor/plugins/myplugin/public/javascripts/myplugin.js. Can I make this available via Rails at /javascripts/myplugin.js?

I've got it working by copying the files from vendor/plugins/______/public/* to public/*, but that seems like a bad idea.


Solution

  • I think this only works if you make your plugin into an engine. Engines can access deeper into the rails initialization process so they can add an additional static asset path.

    Here is a snippet of my engine.rb file that does this:

    module MoxieForum
      class Engine < Rails::Engine
    
        initializer "static assets" do |app|
          app.middleware.use ::ActionDispatch::Static, "#{root}/public"
        end
      end
    end
    

    I recently wrote a handy starting point for creating a rails 3 engine that has this and lots of other basic functionality built in:

    http://keithschacht.com/creating-a-rails-3-engine-plugin-gem