Question

Alright so I have a plugin for Redmine (Rails Engines.) The plugin uses the hooks such that it's HTML/controls get rendered on every page in Redmine.

I am receiving and sending information from this plugin to the controller. Now I have two options I can use in the javascript and css: relative pathing or absolute pathing.

Relative Pathing won't work for every page since the URL paths may anywhere from one to three sections to path from (Since the control must work from every page). This option requires my plugin to be rendered at the same nested level in the URL every time which it is not.

Absolute Pathing works for a particular install, but my plugin breaks when others use it under installs where the path is not under the domain root i.e. www.mysite.com/redmine/ instead of www.mysite.com. This requires manual editing which is not elegant at all.

Is there any way to easily address this seemingly simple issue?

Was it helpful?

Solution

If it's a CSS, JS, or image file you can use the tag helpers with the :plugin option (Rails engines patches these)

stylesheet_link_tag 'redmine_kanban.css', :plugin => 'redmine_kanban'
javascript_include_tag 'jquery-1.3.2.min.js', :plugin => 'redmine_kanban'
image_tag 'example.png', :plugin => 'redmine_kanban'

Make sure that the plugin name matches it's install directory (e.g. vendor/plugins/redmine_kanban in the example above).

To reference assets like images in your css, you can use an external css file and url(../images/example.png. Rails Engines will copy the assets/ directory to public/plugin_assets/plugin_name/[images|javascripts|stylesheets] so you just need go down a directory to get to images.

JavaScript is a bit more difficult. The best way would be to export a JavaScript variable of the current relative path. I do something like this to export the language translation strings to JSON to use in my JavaScript.

javascript_tag("var redmineRootPath = #{Redmine::Utils.relative_url_root}")

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top