设置默认控制器但不影响其他控制器和路径

时间:2014-09-14 06:04:06

标签: ruby-on-rails

我想将root替换为welcome controller,

但是,如果我使用网址http://localhost:3000/welcome/portfolio行动'欢迎'找不到WelcomeController

如何使用路线规则match '/:action(/:id)', :controller => "welcome",via: [:get, :post]

不影响原始控制器

收起路线

                 Prefix Verb     URI Pattern                  Controller#Action
                   root GET      /                            welcome#index
                        GET|POST /:action(/:id)(.:format)     welcome#:action
portfolio_welcome_index GET      /welcome/portfolio(.:format) welcome#portfolio
          welcome_index GET      /welcome(.:format)           welcome#index
                        POST     /welcome(.:format)           welcome#create
            new_welcome GET      /welcome/new(.:format)       welcome#new
           edit_welcome GET      /welcome/:id/edit(.:format)  welcome#edit
                welcome GET      /welcome/:id(.:format)       welcome#show
                        PATCH    /welcome/:id(.:format)       welcome#update
                        PUT      /welcome/:id(.:format)       welcome#update
                        DELETE   /welcome/:id(.:format)       welcome#destroy

route.rb

  root :to => "welcome#index"
  match '/:action(/:id)', :controller =>  "welcome",via: [:get, :post]
  resources :welcome do
    collection do
      get 'portfolio'
    end
  end

1 个答案:

答案 0 :(得分:1)

发生这种情况是因为/:action(/:id)使用格式为/something/some_id甚至/something的每个路径,因此您可以将其放在路径文件的末尾:

 resources :welcome do
    collection do
      get 'portfolio'
    end
  end

  match '/:action(/:id)', :controller =>  "welcome",via: [:get, :post]
  root :to => "welcome#index"

在这种情况下,在转到/welcome/portfolio定义之前,将resources定义处理/:action(/:id)的请求。