Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

URL Generation for routes that point to a Rack application include the controller and action parameters

#4326

Given a route file:

SocketChat::Application.routes.draw do |map|
  resources :channels do
    resources :people
    resources :events do
      collection do
        get :socket, :to => SocketController
      end
    end
  end
end

and the URL generation code

socket_channel_events_url(@channel, :scheme => 'ws')

The following is generated:

ws://localhost:3000/channels/4bb8d4c7b0499803fe000002/events/socket?action=show&controller=channels

When I expected:

ws://localhost:3000/channels/4bb8d4c7b0499803fe000002/events/socket

Reported by Brad Gessler · April 4th, 2010 @ 10:39 PM

State: resolved
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: High

Activity

  1. Rizwan Reza
    Rizwan Reza
    • State changed from new to open
    • Tag changed from rails3 routes to rails3, routes
    • Milestone cleared.

    Can you provide a failing test case?

    April 5th, 2010 @ 02:08 AM

  2. Rizwan Reza
    Rizwan Reza

    I cannot reproduce this. It is correctly generating URLs for a rack app at my side. You can write a failing test case in routing_test.rb.

    April 5th, 2010 @ 02:31 AM

  3. Gaël Deest
    Gaël Deest

    Hi, this might be related. Here is my controller:

        class MyControllerController < ApplicationController
          before_filter { |controller| render :text => controller.url_for(controller.params) }
          def my_action; end
        end
    

    Output:

        http://localhost:3000/my_controller/my_action?action=my_action&controller=my_controller
    

    April 6th, 2010 @ 06:38 PM

  4. Dan Pickett
    Dan Pickett
    • Tag changed from rails3, routes to bugmash, rails3, routes

    May 9th, 2010 @ 06:26 PM

  5. Dan Pickett
    Dan Pickett
    • No changes were found…

    May 9th, 2010 @ 06:27 PM

  6. Bobby Wilson
    Bobby Wilson

    I also could not reproduce this.

    Brad,
    Can you simplify your problem code any?

    May 29th, 2010 @ 12:50 AM

  7. Indrek Juhkam
    Indrek Juhkam

    The problem is :to => SocketController part. Rails doesn't translate it. If you change it to :to => "socket#index" then it works.

    How to reproduce:

    Create Test1Controller and Test2Controller.

    To routes.rb add

     match "test1", :to => "test1#index"
     match "test2", :to => Test2Controller, :as => :test2
    

    Now add an index action to the Test1Controller

     def index
       render :text => test2_url
     end
    

    Go to /test1 url and see the response.

    June 1st, 2010 @ 03:31 PM

  8. Rohit Arondekar
    Rohit Arondekar

    Confirmed on Rails master (on Ruby 1.9.2-head) by following instructions given by Indrek. Also note that test2_path has the same problem. Also the rake routes output:


    test1 /test1 {:controller=>"test1", :action=>"index"}
    test2 /test2

    And just glancing through http://edgeapi.rubyonrails.org/classes/ActionDispatch/Routing.html I don't Rails is expected to handle Test2Controller in :to.

    June 4th, 2010 @ 03:38 PM

  9. Neeraj Singh
  10. Neeraj Singh
    Neeraj Singh
    • Assigned user set to José Valim

    Can someone verify if the patch is working?

    June 25th, 2010 @ 09:12 PM

  11. José Valim
    José Valim
    • State changed from open to wontfix

    -1 for supporting the controller class in the router API (and you don't want that, because it will force ActionController to load if you are using simple stuff like rake tasks).

    June 25th, 2010 @ 11:18 PM

  12. Andrew White
    Andrew White
    • State changed from wontfix to invalid

    Actually it's invalid - you just need to specify the end point.

      resources :channels do
        resources :people
        resources :events do
          collection do
            get :socket, :to => UsersController.action(:show)
          end
        end
      end
    

    The original subject of the ticket is invalid as well:

      andyw$ rails c
      >> app.socket_channel_events_url('1', :protocol => 'ws')
      => "ws://www.example.com/channels/1/events/socket"
    

    June 25th, 2010 @ 11:35 PM

  13. Andrew White
    Andrew White
    • State changed from invalid to open
    • Milestone cleared.
    • Importance changed from to High

    Sorry, this isn't invalid. The problem occurs when you have recall params which get added to the url as there isn't a controller and action when routing to a block, proc or class. Attached patch removes controller and action when the target route isn't a Dispatcher instance.

    June 27th, 2010 @ 08:12 AM

  14. Repository
    Repository
    • State changed from open to resolved

    (from [91b52c795f62aa15505f2f098bc86d6f6db75105]) Normalize recall params when the route is not a standard route otherwise :controller and :action may appear in the generated url [#4326 state:resolved]

    Signed-off-by: José Valim jose.valim@gmail.com
    http://github.com/rails/rails/commit/91b52c795f62aa15505f2f098bc86d...

    June 27th, 2010 @ 08:23 AM

  15. Jeremy Kemper