问题
if I do map.resources :users
in routes.rb, the routes I get have (.:format) at the end of each route (rake routes).
How do I get rid of this in rails 2.3?
I'm pretty sure in 3.1.1 I can do something like :format=>false. Is this available in 2.3? Is there a monkey patch I can do to mimic :format=>false?
Thanks.
回答1:
Monkey patched. Blah. I really, really wanted to change the default behavior, but took into consideration potential future developers' sensitivities.
map.resource(s) ..., :format=>false
now doesn't include the format in the route
config/initializers/resources.rb:
module ActionController
module Resources
private
def map_resource_routes(map, resource, action, route_path, route_name = nil, method = nil, resource_options = {} )
if resource.has_action?(action)
action_options = action_options_for(action, resource, method, resource_options)
formatted_route_path = (resource.options[:format] == false ? route_path : "#{route_path}.:format")
if route_name && @set.named_routes[route_name.to_sym].nil?
map.named_route(route_name, formatted_route_path, action_options)
else
map.connect(formatted_route_path, action_options)
end
end
end
end
end
The change I made is here:
formatted_route_path = (resource.options[:format] == false ? route_path : "#{route_path}.:format")
It used to just be formatted_route_path = "#{route_path}.:format"
To get it to apply to all routes, in routes.rb, I just wrapped all routes with map.with_options :format=>false do |map| ... end
来源:https://stackoverflow.com/questions/7781925/is-it-possible-to-remove-format-from-from-routes-for-my-resources-rails2-3