Need a Rails route with a possible period in the :id, but also retain the optional :format

前端 未结 3 1729
野的像风
野的像风 2021-02-01 05:10

I have a Rails route that takes stock ticker symbols as the :id

  • feeds/AMZN will return a page for Amazon
  • feeds/AMZN.csv will ret
3条回答
  •  别跟我提以往
    2021-02-01 06:09

    Old question, but I found a much simpler way that works with nested routes (I'm on Rails 3.2.4). This way allows all characters (including the dot) as opposed to the accepted answer which makes you specify the allowed charcters.

    resources :feeds, :id => /([^\/])+?/
    

    Note that I had found some other suggestions (e.g. here: http://coding-journal.com/rails-3-routing-parameters-with-dots/) of doing something like:

    resources :feeds, :id => /.*/
    

    but that didn't work for me with nested routes for some reason.

提交回复
热议问题