Can I bind a parameter after an underscore in a rails 2.3 route?
In order to maintain backward compatibility, we need to map /sitemap_1234000.xml to the sitemap controller with 1234000 passed as an ID. 开发者_运维百科I tried
map.connect '/sitemap_:id.xml', :controller => 'sitemap',
:action => 'show'
but then when I do
assert_routing '/sitemap_1234000.xml', :controller => 'sitemap',
:action => 'show',
:id => "1234000"
I get told that no route exists for '/sitemap_1234000.xml'. Can I bind a parameter after an underscore in a rails 2.3 route?
You can use wildcards:
map.connect '/sitemap_(*id).xml, :controller=>'sitemap', :action=>'show'
Then params[:id] should give you what you want.
Not tested - let me know if this works
After some digging around inside rails, this is what I came up with:
config/initializers/bound_parameter_after_underscore.rb
module ActionController
module Routing
class RouteBuilder
def initialize_with_bound_parameter_after_underscore
initialize_without_bound_parameter_after_underscore
@nonseparator_regexp = /\A([^#{Regexp.escape(separators.join)}:\*]+)/
end
alias_method_chain :initialize, :bound_parameter_after_underscore
end
end
end
That stops colon and asterisk from appearing in static segments. Previously the regex which looks for static segments was consuming all of sitemap_:id
; now it consumes sitemap_
and leaves :id
which is then recognised as you would hope.
精彩评论