开发者

Converting routes for rails 3

This is a pretty silly question but I'm having some real trouble figuring out. I want to convert the following routes to make it compliant for Rails 开发者_JS百科3 (from 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

Any help would be much appreciated!


I think I would do it like this:

  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


This is awesome, I just wrote an article about this a couple weeks ago:

Routing in Ruby on Rails 3

It goes over most aspects of the conversion, with a downloadable sample app. While I didn't cover the with_options conversion specifically, I can do a little of that here. Here's a short way:

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

This matches all the routes you have above, and your named routes would look like this:

static_page_path(:faq)
static_page_path(:about)

...and so on. If you want your named routes to still look like static_page_faq_path then you can specify them one at at time, like so:

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

I hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜