Create a link inside a Redmine hook

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

Share

  • Facebook
  • Twitter
  • HackerNews
  • Reddit
  • Tumblr
  • Delicious
  • Email
  • RSS

No related posts.

About Eric Davis

I founded Little Stream Software where I provide Redmine and ChiliProject services to help projects teams. I also created an ebook, Redmine Tips, were I show you how to become more productive using Redmine. I am also the author of Refactoring Redmine, where I go about refactoring Rails using Redmine as an example.

, ,

Chirk HR     Reuse your existing job applicants »
WP Socializer Aakash Web