Y at-il une meilleure façon de faire with_options dans Rails 3 itinéraires?
-
09-10-2019 - |
Question
Voici mes Rails 2 itinéraires:
map.with_options :controller => 'foo', :conditions => { :method => :post } do |foo|
foo.one 'one', :action => 'one'
foo.two 'two', :action => 'two'
foo.with_options :special_flag => 'true', :path_prefix => 'special_prefix',
:conditions => { :method => :get } do |bar|
bar.three '', :action => 'for_blank'
bar.four 'another', :action => 'for_another'
end
end
Comment puis-je convertir ce genre de chose à Rails 3? Il suffit de continuer à utiliser with_options de la même manière? Il devient verbeux dans certains cas, parce qu'au lieu de faire
match '' => 'foo#for_blank'
Je fais
match '', :action => 'for_blank'
La solution
Oui, with_options
fonctionne toujours dans Rails 3. Essayez ceci:
map.with_options :controller => 'foo', :via => :post do
match 'one', :action => 'one' #automatically generates one_* helpers
match 'two', :action => 'two' #automatically generates two_* helpers
foo.with_options :special_flag => 'true', :path => 'special_prefix', :via => :get do
match '', :action => 'for_blank'
match 'another', :action => 'for_another', :as => "four" # as will change the helper methods names
end
end
L'option :via
remplace votre hachage laid conditions
avec une syntaxe beaucoup plus agréable de.
Autres conseils
Comme ceci:
#JSON API
defaults :format => 'json' do
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
resources :users, :sessions
end
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow