在Rails Routes中将控制器操作重定向到root?

时间:2010-06-28 23:52:20

标签: ruby-on-rails ruby routes

我的路线文件中有一个根路径:

root 'games#index'

问题在于,如果有人访问:http://domain.com/games它没有显示根目录,那么我们就会为同一页面创建两个网址。

有没有办法可以将任何匹配更改为http://domain.com/gameshttp://domain.com

如果在路径文件夹中有一个很好的方法,我宁愿不在应用程序控制器中使用before_filter

谢谢!

4 个答案:

答案 0 :(得分:3)

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

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

虽然,问题确实是/games路线不应该在那里。

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

答案 1 :(得分: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('/')

答案 2 :(得分:0)

从当前版本的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

答案 3 :(得分:0)

<error message="You can't access this resource as it requires an 'view' access for the website id = 3."/>

routes.rb

在控制器中:

  root 'games#index'