Defect #19024

link_to in Redmine::Hook::ViewListener omits url root

Added by Jakob Gillich about 3 years ago. Updated about 3 years ago.

Status:ClosedStart date:
Priority:NormalDue date:
Assignee:Jean-Philippe Lang% Done:

0%

Category:Plugin API
Target version:3.0.0
Resolution:Fixed Affected version:

Description

Linking to a controller action omits the configured url root (RAILS_RELATIVE_URL_ROOT environment variable).

For example, put the following code in a plugin & set RAILS_RELATIVE_URL_ROOT=/redmine:

link_to "Test", { :controller => 'my', :action => 'plugin' }
# => <a href="/my/plugin">Test</a>
# Should be: href="/redmine/my/plugin" 

There are quite a few others that seem to have the same problem:
https://github.com/peelman/my_projects/issues/5
https://stackoverflow.com/questions/25315103/link-to-omits-redmine-root

I am not sure if this is only reproducible under specific environments (at least the my_projects author doesn't seem to have this problem); I tried both the Bitnami Installer on Windows and the sameersbn/redmine Docker container. You should get the same results with the following steps:

  1. Download https://github.com/peelman/my_projects
  2. On Windows: Install Redmine using Bitnami and put the plugin in the plugins folder
    On Linux: Install Docker & fig, put this in the addon folder and type fig up.
  3. Open Redmine, create a project and go to the Home page.
  4. Observe that all links generated by the plugin don't include the relative URL.

Associated revisions

Revision 13960
Added by Jean-Philippe Lang about 3 years ago

link_to in Redmine::Hook::ViewListener omits relative url root (#19024).

History

#1 Updated by Toshi MARUYAMA about 3 years ago

  • Subject changed from link_to omits url root in plugins to link_to in Redmine::Hook::ViewListener omits url root in plugins on thin
  • Category set to Plugin API

#2 Updated by Toshi MARUYAMA about 3 years ago

  • Subject changed from link_to in Redmine::Hook::ViewListener omits url root in plugins on thin to link_to in Redmine::Hook::ViewListener omits url root in plugins

Google "Redmine::Hook::ViewListener suburi".
http://goo.gl/A95cJa

On Windows bitnami.

C:\Bitnami\redmine-2.6.0-1\apps\redmine\htdocs>bundle exec thin
  -e production -a localhost -p 3000 --prefix /redmine start

diff --git a/lib/my_projects/view_hook_listener.rb b/lib/my_projects/view_hook_listener.rb
index fc404d8..f099f08 100644
--- a/lib/my_projects/view_hook_listener.rb
+++ b/lib/my_projects/view_hook_listener.rb
@@ -42,10 +42,21 @@ class MyProjects < Redmine::Hook::ViewListener

       return html
   end
-  
+
+  def suburi(url)
+    baseurl = Redmine::Utils.relative_url_root
+    if not url.match(/^#{baseurl}/)
+      url = baseurl + url
+    end
+    return url
+  end
+
   def link_to_project(project)
     html = '<li>'
-    html += "#{link_to h(project.name), { :controller => 'projects', :action => 'show', :id => project } }" 
+    # html += "#{link_to h(project.name), { :controller => 'projects', :action => 'show', :id => project } }" 
+    url = suburi(url_for({ :controller => 'projects', :action => 'show', :id => project}))
+    html += "#{link_to(h(project.name), url)}" 
+
     html += " | #{link_to l(:label_issue_plural), { :controller => 'issues', :action=>'index', :project_id => project } } " 
     html += " | #{link_to l(:label_wiki), { :controller => 'wiki', :action=>'show', :project_id => project } } " 
     html += '</li>'

#3 Updated by Toshi MARUYAMA about 3 years ago

  • Subject changed from link_to in Redmine::Hook::ViewListener omits url root in plugins to link_to in Redmine::Hook::ViewListener omits url root

#4 Updated by Jakob Gillich about 3 years ago

Are you saying that this has to be fixed in the plugins and not in Redmine? That seems wrong to me, the url root is something that Redmine supports by default, you can't possibly expect every single plugin to handle that case. Also, the core code uses just link_to, I guess there is already some logic in place to prepend the url root.

#5 Updated by Jean-Philippe Lang about 3 years ago

  • Status changed from New to Closed
  • Assignee set to Jean-Philippe Lang
  • Target version set to 3.0.0
  • Resolution set to Fixed

This is fixed in r13960. Unfortunately, this fix works with Rails 4 only and won't be backported in 2.6 branch.

#6 Updated by Jakob Gillich about 3 years ago

Thanks! I guess I'll just use the workaround until then.

Also available in: Atom PDF