Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to DRY up routes in Rails 3

In Rails 3, given routes like

get 'about/terms', :as => 'terms'
get 'about/privacy', :as => 'privacy'
get 'about/jobs', :as => 'career'
get 'about/feedback', :as => 'feedback'
get 'about/contact', :as => 'contact'
get 'about/us', :as => 'about'

How to DRY it up?

like image 482
Zabba Avatar asked Nov 21 '25 21:11

Zabba


2 Answers

Recon something like this would do it:

['terms', 'privacy', 'jobs', 'feedback', 'contact' ,'us'].each { |r|
    get "about/#{r}", :as => r 
}
like image 158
scaney Avatar answered Nov 24 '25 00:11

scaney


if about is a controller or you hava a controller for your static pages

  ['terms', 'privacy', 'jobs', 'feedback', 'contact' ,'us'].each { |r|
      get "/#{r}", :controller => 'about', :action => r
  }
like image 26
utiq Avatar answered Nov 24 '25 01:11

utiq