Reputation: 882
I have a root path in my routes file:
root 'games#index'
The problem is that if anyone goes to: http://domain.com/games it doesn't show the root, thus we're creating two urls for the same page.
Is there a way to change any hit to http://domain.com/games to http://domain.com?
I'd rather not fiddle around with a before_filter
in the application controller if there's a nice way to do it in the routes folder.
Thanks!
Upvotes: 4
Views: 8098
Reputation: 5333
routes.rb
:
root 'games#index'
In controller:
def index
redirect_to root_path if request.env['PATH_INFO'] === '/games'
@games = Game.all
end
Upvotes: 2
Reputation: 3668
As of the current version of Rails (3.2.9), this is how I achieved the same result:
MyApp::Application.routes.draw do
# ...
# declare the redirect first so it isn't caught by the default
# resource routing
match '/games' => redirect('/')
# ...
resources :games
# ...
root :to => 'games#index'
end
Upvotes: 0
Reputation: 24541
I had the same issue with sessions. I wanted to expose /sessions for login and logout, but since an invalid password leaves you at /sessions, I wanted a graceful way to deal with a user re-submitting that URL as a GET (e.g. by typing Ctrl-L, ). This works for me on Rails 3:
resources :sessions, :only => [:new, :create, :destroy]
match '/sessions' => redirect('/login')
match '/login', :to => 'sessions#new'
I think your code would look something like this:
resources :games, :only => [:new, :edit, :create, :destroy]
match '/games' => redirect('/')
Upvotes: 3
Reputation: 18350
The easiest way is to just set up a redirect.
map.redirect('/games','/')
Although, your problem is really that the /games
route shouldn't be there in in the first place.
Remove the catchall route at the bottom of your routes.rb
file, and this won't even be a problem.
Upvotes: 3