2010-06-28 72 views

回答

3

最简单的方法就是设置重定向。

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

虽然,你的问题是真的在首位/games路线不应该在那里。

删除routes.rb文件底部的catchall路径,这甚至不会成为问题。

+1

Jamie建议关于从routes.rb中删除路由的建议。然后,只要避免将任何代码链接到'/ games'路径,就可以使用'root_path'辅助方法。 – Karl 2010-06-30 00:57:32

+0

嗨,大家好,所以我有: map.resources:游戏 map.root,:控制器=> '游戏',:动作=>“指数 的事情是,我需要显示,创建新的,破坏了游戏,但索引应始终是根路径。 我试过重定向,但我得到这个错误: 没有路由匹配“/” 有没有办法map.resources:游戏,但不是索引? – kineticac 2010-07-04 02:58:55

+0

此外,我需要这个的原因并不是因为我按照说的方式链接到它,而是我有一个来自不同域的永久重定向,当重定向时,将URL作为domain.com/games而不是根传递。如果使用ENV [“RAILS_ENV”]!='development'&& request.env ['HTTP_HOST']!= CURRENT_DOMAIN request.parameters [:host] = CURRENT_DOMAIN perm_redirect_to url_for参数) end – kineticac 2010-07-04 03:02:02

3

我和会话有同样的问题。我想公开/会话以进行登录和注销,但由于无效密码会将您留在/ sessions,因此我想要一种优雅的方式来处理用户重新将该URL作为GET(例如,通过键入Ctrl-L)的方式。这为我工作on 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('/') 
+0

'match'/ games'=> redirect('/'),:via =>:get' – hlcs 2015-10-05 17:27:34

0

由于导轨(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