Forouzani
Forouzani

Reputation: 13

Rails routing with a letter as separator

I am looking to create a route which does image resizing, and I want it to be of the following format:

/image/1024x768/hello.jpg

and I need to collect the width and height out dynamically, as such, I need routing to be something like:

/image/:widthx:height/:name.jpg

obviously this won't work because :width would be mixed as :widthx

I have tried searching for solution to this, but I can't seem to find one - surely rails routing isn't so rigid that it doesn't faciliate this?

Also, I don't want to set "x" as a "separator" as that would mean I can't use it as part of the image filename.

Any ideas of how I can set up this routing?

Upvotes: 1

Views: 371

Answers (1)

Matt
Matt

Reputation: 17649

You can enclose the named parameters in brackets:

match 'resize/(:width)x(:height)/:image',
      :width => /\d+/, :height => /\d+/,
      :to => 'image#resize'

An URL like /resize/100x400/hello.jpg will end up as the following parameters:

{
 "controller"=>"image",
 "action"=>"resize",
 "width"=>"100", "height"=>"400",
 "image"=>"hello", "format"=>"jpg"
}

Upvotes: 3

Related Questions