转换轨道的路线3

时间:2010-12-10 15:41:09

标签: ruby-on-rails ruby-on-rails-3 routing

这是一个非常愚蠢的问题,但我找出了一些真正的麻烦。我想转换以下路由以使其符合Rails 3(来自2.8.x):

map.with_options :controller => 'static_pages', :action => 'show' do |static_page|
      static_page.faq 'faq', :id => 'faq'
      static_page.about 'about', :id => 'about'
      static_page.blog 'blog', :id => 'blog'
      static_page.support 'support', :id => 'support'
      static_page.privacy 'privacy', :id => 'privacy'
      static_page.howitworks 'howitworks', :id => 'howitworks'
      static_page.contact 'contact', :id => 'contact'
      static_page.terms_and_conditions 'terms_and_conditions', :id => 'terms_and_conditions'
  end

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

这太棒了,几周前我刚刚写了一篇关于这篇文章的文章:

Routing in Ruby on Rails 3

通过可下载的示例应用程序,它涵盖了转换的大多数方面。虽然我没有具体介绍with_options转换,但我可以在这里做一些。这是一个简短的方法:

scope :static_pages, :name_prefix => "static_page" do
  match "/:action", :as => "action"
end

这匹配您上面的所有路线,您的命名路线将如下所示:

static_page_path(:faq)
static_page_path(:about)

......等等。如果您希望您的命名路由仍然像static_page_faq_path那样,那么您可以在时间指定它们,如下所示:

scope '/static_pages', :name_prefix => 'static_page' do
  match '/faq', :to => 'static_pages#faq'
  match '/about', :to => 'static_pages#about'
  # fill in all the rest here
end

我希望这有帮助!

答案 1 :(得分:1)

我想我会这样做:

  scope '/static_pages', :name_prefix => 'static_page', :to => 'static_pages#show' do
    for page in %w{ faq about blog support privacy howitworks contact terms_and_conditions }
      match page, :id => page
    end
  end