I have a CMS style application where the user can set a custom url and it routes to our “content_pages” controller.
To support this we have 3 wildcard routes defined.
I’m trying to constrain these wildcards so that they only respond to requests where the format is html, json, or xml and nothing else. This stems out of a problem where a missing favicon.ico results in a series of queries and web requests because it routes to the content_pages controller and then 404s.
Here’s what I have so far but the constraint simply doesn’t work. (favicon still routes)
get "/:id/edit", to: "content_pages#edit", :constraints => {:id => /.*/, :format => "[html|xml|json]"}, as: :edit_content_page
put "/:id", to: "content_pages#update", :constraints => {:id => /.*/, :format => "[html|xml|json]"}, as: :content_page
get "/:id", to: "content_pages#show", :constraints => {:id => /.*/, :format => "[html|xml|json]"}, as: :content_page
I also tried putting this into a custom constraint class but then the actions on content_pages that arent included here (like /content_pages which routes to index) don’t render.
Here’s the earlier resource statement that wires up the other actions.
resources :content_pages, except: [:get, :edit, :update] do
collection do
get :get_url
end
end
Any thoughts on how i can make this constraint apply without breaking our other, non-constrained actions?
If the only file type you want to exclude is
.ico, then you could update your:idconstraint to explicitly exclude it: