Combining Option Routes in Rails 2.3
-
29-09-2019 - |
Pergunta
I'm tring to setup a Route like so:
GET /settings/ PUT /settings/ GET /settings/photos PUT /settings/photos
This is the routing code that I have setup for it:
#I just do this for code reuse
get = { :method => :get }
put = { :method => :put }
pub.settings '/settings', :controller => :settings, :action => :index, :conditions => get
pub.with_options :controller => :settings, :path_prefix => "/settings", :name_prefix => "settings_" do |settings|
settings.update '', :action => :update, :conditions => put
settings.photos '/photos', :action => :photos, :conditions => get
settings.photos_update '/photos', :action => :photos_update, :conditions => :put
end
This works, but if you notice that the first route "pub.settings" is outside of the mapped_options block.
If I were todo
pub.with_options :controller => :settings, :path_prefix => "/settings", :name_prefix => "settings_" do |settings|
settings.root '', :action => :index, :conditions => get
settings.update '', :action => :update, :conditions => put
settings.photos '/photos', :action => :photos, :conditions => get
settings.photos_update '/photos', :action => :photos_update, :conditions => :put
end
Then I would get (in rake routes) the path to settings would be "settings_root_path" and not "settings_path"
Does anyone know how to include it into the block and still have the routing function name as "settings_path" ?
Solução
Rails has a :path_prefix
, :path_names
, and :name_prefix
that help control how the helpers are generated. The rails routing guide has a few examples that might help.
:path_names
controls the names being used in restful routes
:path_prefix
sets the path used when generating helpers.
:name_prefix
sets the prefix to the name of the generated helpers. This prefix may be set to nil
or the empty_string.
For your case, I'd try (though untested):
settings.root '', :action => :index, :name_prefix => nil, :conditions => get
Outras dicas
settings.settings '', :action => :index, :conditions => get, :name_prefix => ''