bug in routes ???

[I had posted to the normal talk mailing list but subsequently thought it perhaps should have been posted here?]

Hi,

Is this a bug in routes.rb, or something I’m doing wrong. Basically from what I can tell the url_for being generated in the case where my existing controller had a
forward slash (i.e.
active_rbac/login) may be causing a problem. I’m creating a link to a
different controller however the URL created has a piece of the
previous controller in it (i.e. active_rbac/). I put a breakpoint in
the view and the run the following to confirm, see below. Also note
the login aspects use a plugin.

Is this a bug?

============== debug / irb =========================
irb(#<#Class:0x39bea80:0x39bea50>):051:0* params
=> {“action”=>“login”, “controller”=>“active_rbac/login”, “locale”=>“en”}

irb(#<#Class:0x39bea80:0x39bea50>):052:0> session[:locale]
=> “en”

irb(#<#Class:0x39bea80:0x39bea50>):053:0> url_for
=> “/en/active_rbac/login/login”

irb(#<#Class:0x39bea80:0x39bea50>):054:0> url_for :locale => “fr”
=> “/fr/active_rbac/login”

irb(#<#Class:0x39bea80:0x39bea50>):055:0> url_for :locale => “fr”, :controller => “splash”

=> “/fr/active_rbac/splash” <== PROBLEM HERE, I expect to see “/fr/splash”

irb(#<#Class:0x39bea80:0x39bea50>):056:0>

You gave url_for a relative path, so it does what is expected: a
relative path construction. What happens if you use an absolute path
?

Bye !

Hi,

I tried passing a controller string of “/contacts” instead of “contacts” and this does work (thanks) however I don’t quite understand the logic around this relative path behaviour in routes.rb.

  • If the controller itself is contacts_controller.rb and it is in the root (i.e. not a subdirectory) why would one be required to specify “/”

  • Conversely what is gained by allowing specification of a relative controller, or more to the point, if one is specified as was the case here what is the logic behind allowing a URL to be greated by routes.rb that does not match a route? Shouldn’t the URL producing mechanism in routes.rb be matching the input parameters against available routes, such invalid URLs are not produced (i.e
    . one that doesn’t match a route)?

PS. In fact using the “/” in the url_for (e.g. url_for :controller => "/contacts
") still seems to be an issue as rails doesn’t resolve it. As an example:

===========routes.rb extract=================
map.connect ‘/login/:en’, :controller => ‘active_rbac/login’, :action => ‘login’

map.splash ':locale', :controller => 'splash', :locale => nil
map.connect ':locale/:controller/:action/:id', :defaults => { :controller => "splash" }

==========manual debug====================

irb(#<#Class:0x38cd400:0x38cd3d0>):005:0* params
=> {“action”=>“login”, “controller”=>“active_rbac/login”, “locale”=>“fr”}

irb(#<#Class:0x38cd400:0x38cd3d0>):006:0> url_for
=> “/fr/active_rbac/login/login”

irb(#<#Class:0x38cd400:0x38cd3d0>):007:0> url_for :controller => ’
contacts’, :action => ‘list’
=> “/fr/active_rbac/contacts/list” <== NOT DESIRED URL, WILL NOT WORK, “active_rbac” should not appear

irb(#<#Class:0x38cd400:0x38cd3d0>):008:0> url_for :controller => ‘/contacts’, :action => ‘list’
ActionController::RoutingError:
No url can be generated for the hash {:action=>“list”, :controller=>“contacts”}
from (druby://localhost:42531) generated_code/routing/generation.rb:198:in `generate_default_path’

    from (druby://localhost:42531) C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/routing.rb:473:in `generate_path'
    from (druby://localhost:42531) C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-

1.12.5/lib/action_controller/routing.rb:469:in generate' from (druby://localhost:42531) C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/url_rewriter.rb:44:in rewrite_path’
from (druby://localhost:42531) C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/url_rewriter.rb:11:in rewrite' from (druby://localhost:42531) C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack- 1.12.5/lib/action_controller/base.rb:488:in url_for’
from (druby://localhost:42531) C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/helpers/url_helper.rb:27:in url_for' from (druby://localhost:42531) (irb):8:in breakpoint’

    from (druby://localhost:42531) C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/breakpoint.rb:512:in `breakpoint'
    from (druby://localhost:42531) C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-

1.1.6/lib/breakpoint.rb:512:in breakpoint' from (druby://localhost:42531) ./script/../config/../app/views/layouts/site_layout.rhtml:38:in _run_rhtml_layouts_site_layout’
from (druby://localhost:42531) C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-
1.12.5/lib/action_view/base.rb:316:in compile_and_render_template' from (druby://localhost:42531) C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:292:in render_template’
from (druby://localhost:42531) C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_view/base.rb:251:in render_file' from (druby://localhost:42531) ./script/../config/../config/nested_layout.rb:7:in inside_layout’

    from (druby://localhost:42531) ./script/../config/../app/views/layouts/login_layout.rhtml:9:in `_run_rhtml_layouts_login_layout'... 49 levels...
    from C:/InstantRails/ruby/lib/ruby/1.8/drb/drb.rb:1512:in `perform'

    from C:/InstantRails/ruby/lib/ruby/1.8/drb/drb.rb:1586:in `main_loop'
    from C:/InstantRails/ruby/lib/ruby/1.8/drb/drb.rb:1582:in `main_loop'
    from C:/InstantRails/ruby/lib/ruby/1.8/drb/drb.rb:1578:in `main_loop'

    from C:/InstantRails/ruby/lib/ruby/1.8/drb/drb.rb:1427:in `run'
    from C:/InstantRails/ruby/lib/ruby/1.8/drb/drb.rb:1424:in `run'
    from C:/InstantRails/ruby/lib/ruby/1.8/drb/drb.rb:1344:in `initialize'

    from C:/InstantRails/ruby/lib/ruby/1.8/drb/drb.rb:1624:in `start_service'
    from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/breakpoint_client.rb:146
    from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-

1.1.6/lib/breakpoint_client.rb:145
from C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in require' from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in require’

    from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/commands/breakpointer.rb:1
    from C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require'
    from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-

1.3.1/lib/active_support/dependencies.rb:147:in `require’
from script/breakpointer:3irb(#<#Class:0x38cd400:0x38cd3d0>):009:0>

Hi,

I’m still battling this one and am yet unsure whether this is a bug or not. It would be great if someone could put me out of my misery and confirm whether this is bug or not?

With the latest testing to characterise the problem it seems to be something to do with (a) controllers a sub-directory down &/or (b) url_for usage in a layout perhaps. That is in the sense that I have a “url_for” which resolves find when the context is one controller but throws an error when in the context of another.

Key points include:

  1. routes.rb routes a URL to a controller which is down 1 subdirectory - Example
    map.connect ‘:locale/mygoodness’, :controller => ‘highlevel/midlevel’, :action => ‘testaction’, :defaults => {:locale => nil}

    map.connect ‘:locale/contactbackdoor’, :controller => “contacts”, :defaults => {:locale => nil}

  2. controller picks this up ok
    class Highlevel::MidlevelController < ApplicationController
    def testaction

    puts “Highlevel::MidlevelController - testaction”
    end

end

  1. the “views/highlevel/midlevel/testaction.rhtml” view is small as the
    application.rb has a layout “site_layout” line.

  2. The “site_layout” has

        <%= link_to "2-contacts ", :controller => 'contacts' %>
    
  3. The site_layout link is generator OK when it is used from the opening page, BUT when it is utilised after a URL has come in via the controller which is down a sub-directory (
    i.e. 'highlevel/midlevel) an error is encountered. See below.

  4. Note the hash indicated in the error below has ":controller=>" highlevel/contacts" and it has seemed to munge the “highlevel/midlevel” controller name with the “contacts” controller name.

  5. Interestingly if I change the “url_for” line in the layout to:

  • <%= link_to “2-contacts “, :controller => nil %> one sees the following in the error `:controller=>”
    highlevel/highlevel/midlevel”

    `

  • <%= link_to "2-contacts ", :controller => “” %> one sees the following in the error

    :controller=>"highlevel/"

Hi,

I'm still battling this one and am yet unsure whether this is a bug or not.
It would be great if someone could put me out of my misery and confirm
whether this is bug or not?

With the latest testing to characterise the problem it seems to be something
to do with (a) controllers a sub-directory down &/or (b) url_for usage in a
layout perhaps. That is in the sense that I have a "url_for" which resolves
find when the context is one controller but throws an error when in the
context of another.

If you want your url_for calls to work from both the top level, and
controllers in modules, you need to use:

:controller=>'/the_controller'

Otherwise it assumes it's a relative link and generates links
accordingly. Another (simpler) option is just to use a named route.

creating subdirectories inside the controller directory is just gonna
create problem some time or another. I’m not surprise to see this
problem.

I’m still following up on Michael’s suggestion but the context of the issue for me was use of the “activeRBAC” plugin/engine which does use the multiple level (e.g. plugin name = active_rbac, and under the app/controller they have a subdirectory called “active_rbac” where the controllers reside. Also I am housing links to these controllers in a common layout rhtml file hence the need for it to work from various spots.

creating subdirectories inside the controller directory is just gonna create
problem some time or another. I'm not surprise to see this problem.

We make extensive use of controllers in modules and have had very few
problems. Tobi at shopify makes extensive use too. We've had no
trouble and I'm not sure what you're basing your opinion on.

Greg's situation is complicated by the use of engines, perhaps ask on
their mailing list. Can you reproduce it without engines Greg?

Interesting…I’ve been going through step by step with a small test app:

  1. multiple sub-directory in standard application => works OK using absolute “/” at the front of the controller value specified in a “link_to”
  2. installed engine plugin and moved the multiple sub-directory controller/view to my test engine => worked ok again with absolute value
  3. moved the links (link_to’s) from the individual views to an application wide layout => seemed to work ok again
  4. introduced the :locale into the routes, and after some adjustments (i.e. putting in a default :locale of nil for each route) got this working too
    In short using the (a) absolute path in the “link_to” and (b) appropriate use of a default :locale of nil, seem to have things working in a small test app. I was getting errors previously in my full blown application when playing with some of these tweaks so I’m not sure what went wrong :frowning: I’ll go back to my full blown application again and see if I can spot something.

Any comments?

Cheers
Greg

PS routes.rb file from test application

I think I have it! I’ve reproduced this without using a plugin or engines, so the behaviour is to do with rails. Question is whether this is expected behaviour or not? Or whether it should be expected behaviour for rails:

Assumptions

[1] To allow a link_to entry to be populated in an application wide layout it needs to work from anywhere. The suggested mechanism to achieve this is by putting a “/” at the front of the “:controller => ’
/xxx’” part of “link_to” to make it absolute

[2] If a route starts with say “:locale” (e.g. “map.connect ‘:locale/grandad’, etc”) then when constructing a link_to there is no need to put this in place as it should be automatically populated if there is a ‘:locale’ variable in params. This seems to generally work.

Issue

[3] When a “link_to” entry in a view file uses the absolute path approach (from [1] above) it would seem that it then prevents the concept described in [2] from occuring.

Possible Workarounds

[4] Placing a default value of :locale in the routes.rb route gets the link_to to generate however obviously that won’t really help by hard-coding a specific locale in. That is this routes.rb default seems to be used in the link_to generation, even though an available params[:locale] parameter will not.

[5] Placing dynamically generated value for :locale in every link_to. Not ideal from a DRY concept. Example:
link_to ‘my link’, { :locale => ‘#{params[:locale]}’, :controller => ‘/parent/child’, :action => ‘testaction’ }

Resolution

[6] Update to rails (
routes.rb??) to allow a “params” value to be used when constructing a link_to or url_for even when the specified controller is an absolute path (e.g. / ).

Aside - I still don’t really understand why rails would allow the concept of specifying relative and absolute controllers? Shouldn’t they all be absolute?

Comments? Do people agree?

Tks
Greg

Hello Greg,

thanks Francois - this worked for me. I think my suggestion still probably stands however no? i.e. there would be situations where this may still be required.