代码之家  ›  专栏  ›  技术社区  ›  Mithun Sreedharan Kuldeep Modi

控制器中的RoR-索引方法

  •  1
  • Mithun Sreedharan Kuldeep Modi  · 技术社区  · 14 年前

    我得到这个URL错误 http://localhost:3000/dashboard/

    Routing Error No route matches "/dashboard"

    但是 http://localhost:3000/dashboard/index 工作正常

    我如何才能使下面的所有url都工作并显示相同的视图 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 回复  |  直到 14 年前
        1
  •  5
  •   Eimantas    14 年前
    Mytest::Application.routes.draw do
      match "/dashboard", :to => 'dashboard#index'
      root :to => "dashboard#index"
    end
    

    您还可以添加 /:controller/:action/:id(.:format) 风格。

        2
  •  1
  •   Max Williams    14 年前

    我会的

      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'
    
        3
  •  0
  •   Kieran Senior    14 年前
    match '/dashboard', :controller => 'dashboard', :action => 'index'
    

    也许?不过我还没有测试过。