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.

Routing: 2.3 provides no way to require a :format

#2772

Commit #fef6c32 (Added optimal formatted routes to rails, deprecating the formatted_ methods, and reducing routes creation by 50%) solves the problem of redundancy when creating routes which can optionally have a format/file-extension, but removes the ability to require that a format be explicitly provided.

# config/routes.rb

map.connect "sitemap.:format", :controller => "pages", :action => "sitemap"


# app/controllers/pages_controller.rb

def sitemap
    respond_to do |format|
        format.xml
        format.text
    end
end

In 2.2.2, the above route coupled with the respond_to block produced the following responses:

/sitemap      404 Not Found
/sitemap.html 406 Not Acceptable
/sitemap.txt  200 OK
/sitemap.xml  200 OK

In 2.3.2, however, the route where no specific format is requested (*/*) remains accessible, displaying the xml document:

/sitemap      200 OK
/sitemap.html 406 Not Acceptable
/sitemap.txt  200 OK
/sitemap.xml  200 OK

This, while not unexpected, remains unintended; and there appears to be no way to make the "all" path unaccessible. --That is, without creating new redundancies (e.g. map.connect("sitemap.xml"), map.connect("sitemap.txt"), ..., etc.)

Further, using :requirements => { :format => /txt|xml/ } in the route doesn't work. --Though it seems using :requirements shouldn't be necessary anyway since routing should probably respect the respond_to block and return 406 Not Acceptable for /sitemap (because format.all is not provided).

Reported by J T Calhoun · June 6th, 2009 @ 11:08 PM

State: duplicate
Milestone: 2.3.4
Assigned to: nobody
Importance: none

Activity

  1. CancelProfileIsBroken
  2. Michael Koziarski
    Michael Koziarski
    • Milestone changed from 2.x to 2.3.4

    June 9th, 2009 @ 09:43 AM

  3. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from format, formats, respond_to, route, router, routes, routing to bugmash, format, formats, respond_to, route, router, routes, routing

    August 7th, 2009 @ 01:58 PM

  4. Adam Bair
    Adam Bair

    Verified. I tried this in 2.2.2 and 2.3.2 and experienced the same behavior as Jason.

    August 9th, 2009 @ 10:22 PM

  5. Adam Bair
    Adam Bair

    I've attached a patch that includes a test case that exposes this bug in 2.3.2.

    August 9th, 2009 @ 10:28 PM

  6. CLR
    CLR

    I was Adam's paired partner for this. Just sayin.

    August 9th, 2009 @ 10:32 PM

  7. Rizwan Reza
    Rizwan Reza

    verified

    +1 This bug exists. The test case fails.

    August 9th, 2009 @ 10:53 PM

  8. Adam Bair
    Adam Bair

    I meant to put a +1 with my verified comment earlier ;)

    August 9th, 2009 @ 11:43 PM

  9. josh
    josh
    • State changed from new to duplicate

    If we can fix :requirements on :format, we can have hacky support for required formats in 2.3. That permanent optionalize was a big changed and caused some annoying side effects. In 3.0, we will have real syntax to optionalize segments.

    Please follow up on #2151 routing not recognizing requirements for :format

    August 10th, 2009 @ 02:26 AM

  10. Jeremy Kemper
    Jeremy Kemper
    • Tag changed from bugmash, format, formats, respond_to, route, router, routes, routing to format, formats, respond_to, route, router, routes, routing

    August 10th, 2009 @ 07:10 AM