ruby on rails - How to make url_for return slashed parameters rather than (? & based parameters) -
rails 2.3.5:
routes:
map.connect ':controller/:action/:sort_key/:sort_order'
options:
{"controller"=>"sections", "action"=>"list", "params"=>{"sort_key"=>"sect_code", "sort_order"=>"asc"}}
url_for(options)
give me:
/sections/list/sect_code/asc
in rail 4.2,
routes:
':controller/:action/:sort_key/:sort_order'
url_for(options)
(same options) give me,
/sections/list?sort_key=sect_code&sort_order=asc
is difference intended? possible make behaviour same?
thanks.
the behaviour intended. when add parameter url_for options isn't controller, action or expected property (eg. trailing_slash: true) url_for helper generate string parameters appended in 'get' fashion. don't know of inbuilt way modify string output become slashes instead of parameters, since html encoded string produced, away using gsub , regex on resulting string.
i create helper method modifies result of url_for method following:
def url_for_with_slashes(options=nil) url_for(options).gsub(/(\?|&)[a-za-z0-9_]+=/, "/") end
its not fool proof, can update regex if find slips net.
edit: on reflection, assign name route in routes.rb file
get ':controller/:action/:sort_key/:sort_order', as: 'sort'
...and generate url slashes using sort_path(sort_key, sort_order)
Comments
Post a Comment