RoR - 控制器中的索引方法

时间:2010-11-18 06:44:14

标签: ruby-on-rails routes

我收到了网址http://localhost:3000/dashboard/

的错误

Routing Error No route matches "/dashboard"

http://localhost:3000/dashboard/index工作正确

如何使以下所有网址生效并显示相同的视图views/dashboard/index.html.erb

http://localhost:3000
http://localhost:3000/
http://localhost:3000/dashboard
http://localhost:3000/dashboard/
http://localhost:3000/dashboard/index

我的路线档案是

Mytest::Application.routes.draw do
  get "dashboard/index"
  root :to => "dashboard#index"
end

我的控制器文件是

class DashboardController < ApplicationController
  def index

  end

end

3 个答案:

答案 0 :(得分:5)

Mytest::Application.routes.draw do
  match "/dashboard", :to => 'dashboard#index'
  root :to => "dashboard#index"
end

您还可以添加/:controller/:action/:id(.:format)样式的通用路线。

答案 1 :(得分:1)

我愿意

  map.root :controller => "dashboard", :action => "index"
  map.dashboard "/dashboard", :controller => "dashboard", :action => "index"
  map.connect "/dashboard/:action", :controller => "dashboard", :action => "index"
  #standard routes
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'

或者你可以使它更标准

  #special case
  map.root :controller => "dashboard", :action => "index"

  #general case
  map.connect ":controller", :action => "index"
  map.connect ":controller/:action"

  #standard routes
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'

答案 2 :(得分:0)

match '/dashboard', :controller => 'dashboard', :action => 'index'

也许?我没有测试过这个。