views:

50

answers:

1

In my application, there is a resource, machine, and a nested resource from machine: ip.

I want to be able to access the URI of an Ip typing the ip address.

The URI should be something like this:

/machines/m123/ips/192.168.0.1.xml

Where "m123" is the name of the machine and "192.168.0.1" is one of the ips of that machine.

The problem here is that rails miss understand the dots from the ip and the format. When I try to access this page, i get:

No route matches "/machines/m123/ips/192.168.0.1.xml"

And if I replace the dots for any other character it works, witch means that rails isn't handling the dots on the URI.

Is there any way to enter a more complex regexp on the routes to make sure rails can treat it the way I want?

I'm using rails 2.3.5 and ruby 1.8.7.

Thank you.

+2  A: 

Use the :requirements => { :param_name => pattern_regex } param:

IP_PATTERN = /\A(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)(?:\.(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)){3}\z/.freeze
map.resources :ip, :requirements => { :id => IP_PATTERN }
clyfe
The pattern is not right, but it does the job. Thank you.
robertokl
Are you sure the pattern is not right? Can you name the problem or provide an example to prove it?
clyfe