2012-02-22 74 views
1

我剛剛通過觀看railscasts升級到rails 3.1.0和3.2.0來升級rails 3.0.9應用程序到rails 3.2.1。更新前我(當時的工作)的routes.rb文件看起來像:使用導軌進行路由3.2.1

match "home" => "pages#index" 
match "*page" => "pages#show" 
root :to => "pages#index" 

我改變了我的routes.rb文件更新後:

match "home" => "pages#index" 
match "pages/*page" => "pages#show", :format => false 
root :to => "pages#index" 

我在潰敗了這個想法從Ruby on Rails routing Globbing部分,以模仿rails 3.0.x.現在

當我我的鏈接,通過呈現在點擊:

<li><%= link_to "What We Do", "/what-we-do" %></li> 

我得到這個錯誤:

Routing Error 
No route matches [GET] "/what-we-do" 
Try running rake routes for more information on available routes. 

rake routes produces: 

    home /home(.:format) pages#index 
      /pages/*page pages#show 
    root/    pages#index 
If that is helpful. 

我試圖渲染頁面叫什麼,我們-do.html .erb,位於app/views/pages。

,這是我的控制器:

class PagesController < ApplicationController 

    def index 
    render :home, :layout => false 
    end 

    def show 
    render static_page 
    rescue 
    # page_not_found 
    end 


    private 

    def static_page 
    "#{RAILS_ROOT}/app/views/pages/#{params[:page]}.html.erb" 
    end 

    def page_not_found 
    render "#{Rails.root}/public/404.html", :layout => false 
    end 

end 

任何人有什麼建議?

+1

。我把我的路線改回:'match「* page」=>「pages#show」'但真正的問題出現在我的控制器中。在rails 3.0.X中,你使用RAILS_ROOT,但是在3.1和以上,它是Rails.root。謝謝大家 – scommette 2012-02-22 20:10:54

回答

3

成員路線將需要一個ID,因爲它作爲一個部件上。收集路線並不是因爲它作用於一組對象。

map.resources :users, :collection => { :customers_list=> :get } 

map.resources :users, :member => { :inactive=> :post } 

所以我想通了這被視爲/users/1;inactive=> [:action => 'inactive', :id => 1]

1

路線必須是這樣的 -

match "home", :to => "pages#index" 
root :to => "pages#index" 
match ":page", :to => "pages#show" 
2

您必須將匹配/pages/what-we-do路線:

/pages/*page pages#show 

但沒有什麼會匹配不/pages/開始URL(當然除了/home)。您仍然需要頂級球圈路線:

match "*page" => "pages#show" 

如果您想匹配/what-we-do。也許你誤解this from the routing guide

Starting from Rails 3.1, wildcard routes will always match the optional format segment by default. For example if you have this route:

match '*pages' => 'pages#show' 

By requesting "/foo/bar.json" , your params[:pages] will be equals to "foo/bar" with the request format of JSON.

的變化是,你不必應付處理自己的格式,現在是自動處理,就像它是所有其他路線。

0

看起來你沒有在路線中提到預期的動作。如果你使用任何的腳手架內的靜態路由,你應該提到在路線的行動中

collection :controller do 
    get 'action name' 
end