Redmine 插件/Rails 引擎 - 每个页面上的插件中图像/javascript 的路径?
好吧,我有一个 Redmine 插件(Rails Engines)。该插件使用钩子,以便在 Redmine 的每个页面上呈现 HTML/控件。
我正在从该插件接收信息并将信息发送到控制器。现在我有两个可以在 javascript 和 css 中使用的选项:相对路径或绝对路径。
相对路径不适用于每个页面,因为 URL 路径可能从一到三个部分到路径(因为控件必须在每个页面上工作)。此选项要求我的插件每次都在 URL 中的相同嵌套级别上呈现,但事实并非如此。
绝对路径适用于特定安装,但当其他人在路径不在域根目录下(即 www.mysite.com/redmine/ 而不是 www.mysite.com)的安装下使用它时,我的插件会中断。这需要手动编辑,一点也不优雅。
有没有什么办法可以轻松解决这个看似简单的问题呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果它是 CSS、JS 或图像文件,您可以使用带有
:plugin
选项的标签助手(Rails 引擎修补这些)确保插件名称与其安装目录相匹配(例如,vendor/plugins/上例中的 redmine_kanban )。
要在 css 中引用诸如图像之类的资源,您可以使用外部 css 文件和
url(../images/example.png
)。Rails Engines 将复制 asset/ 目录到 public/plugin_assets/plugin_name/[images|javascripts|stylesheets] 所以你只需要进入一个目录就可以访问图像。JavaScript有点困难,最好的方法是导出当前相对路径的 JavaScript 变量。我做某事像这样将语言翻译字符串导出到 JSON 以在我的 JavaScript 中使用
希望这会有所帮助。
If it's a CSS, JS, or image file you can use the tag helpers with the
:plugin
option (Rails engines patches these)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.
Hope this helps.