[英]Is there a better way to do with_options in Rails 3 routes?
Here are my Rails 2 routes: 这是我的Rails 2路线:
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
How do I convert this sort of thing to Rails 3? 如何将这种东西转换为Rails 3? Just keep using with_options in the same way? 只是继续以相同的方式使用with_options吗? It becomes wordier in some cases because instead of doing 在某些情况下,它变得更加冗长,因为
match '' => 'foo#for_blank'
I'm doing 我正在做
match '', :action => 'for_blank'
Yeah, with_options
still works in Rails 3. Try this out: 是的, 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
The :via
option replaces your ugly conditions
hash with a much nicer syntax. :via
选项用更好的语法替换了丑陋的conditions
散列。
Like this: 像这样:
#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
Try to stick to the methods the routes provide. 尝试坚持路线提供的方法。 They are very powerful in Rails 3 and should provide everything you need. 它们在Rails 3中非常强大,应该可以提供您所需的一切。 See http://guides.rubyonrails.org/routing.html for more details 有关更多详细信息,请参见http://guides.rubyonrails.org/routing.html 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.