1

Hier sind meine Rails 2 Routen:Gibt es eine bessere Möglichkeit, mit Optionen in Rails 3 Routen zu tun?

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 

Wie konvertiere ich diese Art der Sache 3 zu Rails? Benutze einfach with_options auf die gleiche Weise? Es wird in einigen Fällen wortreicher weil statt

match '' => 'foo#for_blank' 

tun

match '', :action => 'for_blank' 

Antwort

2

Ja, with_options noch in Rails funktioniert die 3. Probieren Sie:

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 

Die :via Option ersetzt Ihr hässliches conditions Hash mit einer viel schönen Syntax.

+0

süß. auch ich kann über und lassen 'post' eins 'und' post 'zwei' ', ja? –

+0

@ John: Das stimmt. –

1

Try Ich mache die Routen bieten die Methoden zu bleiben. Sie sind sehr leistungsfähig in Rails 3 und sollten alles bieten, was Sie brauchen. Siehe http://guides.rubyonrails.org/routing.html für weitere Details

+2

ich gelesen, dass sorgfältig, nicht viel anders als 'scope' gefunden haben, die mir nicht helfen, aber gerade jetzt grub ich ein wenig tiefer in die api und fand die Gleichen von "Controller" Essen "do" ... Das wird den Trick machen! http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Scoping.html#method-i-controller –

+0

hmmm in der Tat, 'Controller' fehlt ... ach ja ... zu viele Methoden in einem Controller wird nicht als in Ordnung betrachtet, also könnte das der Grund sein. Gut zu wissen, dass du trotzdem eine Lösung gefunden hast. – iain

2

So:

#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 
Verwandte Themen