I have a URL http://localhost:3000/features?feature_group=everyone
and i am trying to make the URL http://localhost:3000/features/for-everyone
how this can be done through routes.

- 2,317
- 2
- 25
- 47
-
possible duplicate of [How does Stack Overflow generate its SEO-friendly URLs?](http://stackoverflow.com/questions/25259/how-does-stack-overflow-generate-its-seo-friendly-urls) – Drenmi May 01 '15 at 06:54
4 Answers
You can define desired route as
get '/features/:feature_group' => 'features#index'
So in action, params[:feature_group] will have 'analytics' in your case
Or you may use collection routes e.g.
resources :features do
collection do
get :feature_group
end
end
So you need a feature_group action in your features controller. To learn about rails routing, ref to http://guides.rubyonrails.org/routing.html

- 2,767
- 1
- 22
- 31
Personally I would use your initial feature
route leading to your index controller and do a nested route within it using a collection for example:
resources :features do
collection do
get :feature_group
get :feature_xxxxx
end
end
What is cool is that in your controller you can do if params[:feature_group] is nil? or present? or equal to "some words" render your index page with different records or else depending on what you want to do.

- 559
- 8
- 21
Okay i am able to do it with the help of constraints
get 'features' => redirect('/features/for-everyone'), :constraints => lambda{|req| req.params["feature_group"] == "everyone"}
get 'features/for-everyone' => "Features#index"
but in the process params["feature_group"] gets lost. So i needed to pass the params in routes using defaults
get 'features/for-everyone' => "Featues#index", :defaults => { :feature_group => "everyone" }

- 2,317
- 2
- 25
- 47
Try this
get 'features' => redirect('/features/for-everyone?%{params}'), :constraints => lambda{|req| req.params["feature_group"] == "everyone"}
or
redirect('/features/for-everyone?feature_group=%{feature_group}'), :constraints => lambda{|req| req.params["feature_group"] == "everyone"}
I tested both this scenario and its working fine.

- 1,515
- 1
- 11
- 21

- 130
- 11