2010-12-14 18 views
1

這裏是我的Rails 2條路線:有沒有更好的方式來做Rails 3路線中的with_options?

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 

如何轉換這種事到Rails 3?只需以同樣的方式繼續使用with_options?它成爲在某些情況下,因爲與其做

match '' => 'foo#for_blank' 

wordier我做

match '', :action => 'for_blank' 

回答

2

呀,with_options仍然工作在Rails的3.嘗試了這一點:

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 

:via選項有好得多的語法替換你的醜陋conditions哈希值。

+0

甜美。我也可以通過並且做'post'one''和'post'two'',是嗎? – 2010-12-14 02:34:46

+0

@John:是的。 – 2010-12-14 05:05:21

1

嘗試堅持的路線提供方法。它們在Rails 3中非常強大,並且應該提供您需要的一切。見http://guides.rubyonrails.org/routing.html更多細節

+2

我讀仔細,沒有發現比'scope'很多其他不幫我,但我剛纔挖的API得更深一些,發現喜歡'控制器'食物'做'...這會做的伎倆! http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Scoping.html#method-i-controller – 2010-12-14 01:31:09

+0

嗯的確,'controller'缺少......哦,好吧......控制器中有太多的方法不被認爲是好的,所以這可能是原因。很高興知道你找到了解決方案。 – iain 2010-12-14 01:38:23

2

像這樣:

#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 
相關問題