Skip to content

Instantly share code, notes, and snippets.

@dustinsmith1024
Created May 17, 2013 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustinsmith1024/5600855 to your computer and use it in GitHub Desktop.
Save dustinsmith1024/5600855 to your computer and use it in GitHub Desktop.
A way to filter a route based on query params. You can also make the module a class and then initialize it to pass in whatever you want.
module QueryParamConstraint
extend self
def matches?(request)
request.query_parameters["view"] == request.path_parameters[:action]
end
end
MyPortal::Application.routes.draw do
root to: 'home#index'
get "/foo" => "people#show", :constraints => QueryParamConstraint
get "/foo" => "people#index", :constraints => QueryParamConstraint
resources :people, only: [:index, :show] do
resources :facts, only: :index
end
end
@dustinsmith1024
Copy link
Author

Requests would look like.

/foo?view=show
/foo?view=index

We could set up a different query_param for nested routes

/foo?view=index&controller=demographics
/foo?view=show&controller=kids&kid_id=3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment