I was recently asked how do create a link in a Redmine plugin hook. The answer is you can use the Ruby on Rails helper link_to
but you need a few additional fields for Rails to know where to link to.
The standard link_to will generate an ActionView::TemplateError
:
1 2 3 4 |
link_to("My Link", { :controller => 'my_controller', :action => 'index' }) |
ActionView::TemplateError (Missing host to link to! Please provide :host parameter or set default_url_options[:host]) on line #7 of issues/_sidebar.rhtml:
Redmine has a Setting that stores the host the server is running on, so you can change the link_to
method to:
1 2 3 4 5 |
link_to("My Link", { :controller => 'my_controller', :action => 'index', :host => Setting.host_name }) |
You might also want to add the :protocol
option, otherwise the generated link will always be http even if the rest of the site is using https. Once again, Redmine has a setting for this:
1 2 3 4 5 6 |
link_to("My Link", { :controller => 'my_controller', :action => 'index', :host => Setting.host_name, :protocol => Setting.protocol }) |
I hope this helps explain how to add links into Redmine’s plugin hooks. If you have any questions, ask me in the comments on this article.
Update: Peter just posted an easier way to create a link using the only_path
option. According to my Rails API docs it it supposed to default to true but the documentation might be wrong (wouldn’t be the first time)
1 2 3 4 5 |
link_to("My Link", { :controller => 'my_controller', :action => 'index', :only_path => true }) |
Eric
Why not just:
No need for host and protocol.
@Peter: You are right, the `:only_path` option works. According to the Rails API documentation it’s supposed to be true by default but maybe the documentation is wrong. I’ve added an update to the post. Thanks.