Does Rails Routes redirect controller action to root?

I have a root path in the routes file:

root 'games#index'

The problem is that if someone accesses: http://domain.com/games , he does not show the root, so we create two URLs for the same page.

Is there a way to change any hit on http://domain.com/games to http://domain.com ?

I would prefer not to discuss with before_filterin the application controller if there is a good way to do this in the routes folder.

Thank!

+3
source share
4 answers

The easiest way is to simply set up a redirect.

map.redirect('/games','/')

, /games , .

routes.rb , .

+3

. / , /, URL GET (, Ctrl-L). Rails 3:

resources :sessions, :only => [:new, :create, :destroy]
match '/sessions' => redirect('/login')
match '/login', :to => 'sessions#new'

, :

resources :games, :only => [:new, :edit, :create, :destroy]
match '/games' => redirect('/')
+3

Rails (3.2.9), :

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
0

routes.rb:

  root 'games#index'

:

  def index
    redirect_to root_path if request.env['PATH_INFO'] === '/games'
    @games = Game.all
  end
0

Source: https://habr.com/ru/post/1752172/


All Articles