Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Rails Routes redirect controller action to root?

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!

like image 911
Art C Avatar asked Feb 27 '23 18:02

Art C


2 Answers

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.

like image 159
Jamie Wong Avatar answered Mar 08 '23 00:03

Jamie Wong


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('/')
like image 35
Paul A Jungwirth Avatar answered Mar 07 '23 22:03

Paul A Jungwirth