2012-02-22 47 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