【问题标题】:Converting routes for rails 3转换rails 3的路线
【发布时间】:2010-12-10 15:41:09
【问题描述】:

这是一个非常愚蠢的问题,但我很难弄清楚。我想转换以下路线以使其符合 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

任何帮助将不胜感激!

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

      【解决方案2】:

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

      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
      

      我希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多