Defect #15872

url_for causes routing problems if a plugin uses namespaces

Added by eyal R almost 8 years ago. Updated about 5 years ago.

Status:NewStart date:
Priority:NormalDue date:
Assignee:-% Done:

0%

Category:Plugin API
Target version:-
Resolution: Affected version:

Description

Redmine currently uses a hash parameter as the url to pass to url_for in many places
for example - lib/redmine.rb (pusing items to menu)

I have a complex plugin which uses namespaces for controllers and models.
Since url_for keeps the current namespace it will create the wrong url.

You can read more about the problem here:

http://blog.enriquez.me/2010/2/21/namespaced-controller-and-link-to-helper-in-rails/

Please consider adding "/" to controller when using url_for or use restful paths.
Thanks.

redmine_patch_leading_fwd_slash.diff Magnifier - patch for defect 15872 (2.69 KB) Gord Brown, 2015-09-25 18:57


Related issues

Duplicated by Redmine - Defect #22048: Exception throws during plugin rendering Closed

History

#1 Updated by Etienne Massip almost 8 years ago

  • Category set to Plugin API

Would it work when Redmine is deployed on a subURI?

#2 Updated by eyal R almost 8 years ago

I will test.

#3 Updated by Jean-Philippe Lang almost 8 years ago

eyal R wrote:

Please consider adding "/" to controller when using url_for or use restful paths.
Thanks.

A patch is welcome, thanks.

#4 Updated by Gord Brown about 6 years ago

Hi, folks,

I ran into this same issue, so here's a patch. All it does is add the necessary forward slashes in 6 spots. I can't guarantee that that I found every occurrence, but it's enough to get my plugin working, so may also be helpful for others. Doesn't seem to break anything (dangerous words, I know...).

The patch is against trunk/14622.

#5 Updated by Gord Brown about 6 years ago

Should add: I haven't tested this with Redmine installed on a sub-URI. Will do that... so wait a bit before applying the patch. :)

#6 Updated by Gord Brown about 6 years ago

Hi again,

Using Apache and Passenger, it does work while installed on a sub-URI, as far as I can tell. Also seems to work under WEBrick, if you tweak "config.ru":

require ::File.expand_path('../config/environment',  __FILE__)
map '/redmine' do
  run RedmineApp::Application
end

Cheers...

#7 Updated by Toshi MARUYAMA about 6 years ago

How about thin and puma?

#8 Updated by Gord Brown about 6 years ago

I don't know... give them a try and let us know!

#9 Updated by Jérôme BATAILLE about 5 years ago

Another issue linked to namespaced controllers is Menu items that use hashed urls with the controller field like :

  menu.push :my_account, { :controller => 'my', :action => 'account' }, :if => Proc.new { User.current.logged? }

To fix that, you can patch MenuHelper like this :

module MyPlugin
  module MenuHelperPatch
    # Method overriden
    def extract_node_details(node, project=nil)

      item = node
      url = case item.url
      when Hash
        url_hash = item.url.dup

        # Specific : manage controllers in namespaces
        if url_hash.has_key?(:controller) && !url_hash[:controller].starts_with?('/')
          url_hash[:controller] = "/#{url_hash[:controller]}" 
        elsif url_hash.has_key?('controller') && !url_hash['controller'].starts_with?('/')
          url_hash['controller'] = "/#{url_hash['controller']}" 
        end
        # END -- Specific : manage controllers in namespaces

        project.nil? ? url_hash : {item.param => project}.merge(url_hash)
      when Symbol
        if project
          send(item.url, project)
        else
          send(item.url)
        end
      else
        item.url
      end
      caption = item.caption(project)
      return [caption, url, (current_menu_item == item.name)]
    end
  end
end

Redmine::MenuManager::MenuHelper.send(:prepend, MyPlugin::MenuHelperPatch)
  • Another issue is the layouts/base template
    It has to be overriden for the search links (/search)

#10 Updated by Go MAEDA almost 3 years ago

  • Duplicated by Defect #22048: Exception throws during plugin rendering added

Also available in: Atom PDF