regex - Routing constraints don't assign values to a Rails parameter correctly; use '+' to delimit values -
i able support country codes , region codes in application's routes. example:
- /entities/us
- /entities/us+ca
- /entities/us/mn
- /entities/us/mn+wi+ia
- /entities/us+ca/bc+wa
my current route:
"/entities/:country_code/(:region_code)" => "entities#index", :constraints => {:country_code=>/[a-za-z]{2}[\+\,]?/, :region_code=>/[a-za-z]{2}[\+\,]?/} resources :entities
trying /entities/us+ca
results in exception:
# use callbacks share common setup or constraints between actions. def set_entity @entity = entity.find(params[:id]) end application trace | framework trace | full trace app/controllers/entities_controller.rb:79:in `set_entity' request parameters: {"id"=>"us+ca"}
i change route to:
get "/entities/:country_code/(:region_code)" => "entities#index" resources :entities
this allows multiple country , region query work (i.e. us+ca
assigned :country_code
parameter), broke /entities/new
path--new
considered :country_code
parameter.
i'm assuming problem related the regular expressions.
is there regex work needs?
i think regex isn't quite right. 1 have there match 2 characters optionally followed +
or ,
. need allow subsequent character pairs.
try regex: /[a-za-z]{2}(\+[a-za-z]{2})*/
(that matches 2 characters followed 0 or more sequences of +
followed 2 characters).
Comments
Post a Comment