Sauron
Sauron

Reputation: 6657

Rails Route Bug

I am building custom rails routes for methods that I require. I have created the method as:

class RequestsController < ApplicationController
    def mainSearch
        .....etc
    end

and testing the route first with:

match '/mainSearch/:latitude', to: 'requests#mainSearch', via: 'get' 

running: localhost:3000/mainSearch/40.0

worked, but then extending to:

match '/mainSearch/:latitude/:longitude', to: 'requests#mainSearch', via: 'get'
running: localhost:3000/mainSearch/40.0/80.0

resulted in the error No route matches [GET] "/mainSearch/40.0/80.0"

even though in the defined rails routes it clearly notes that the route exists:

enter image description here

This route should clearly work, but it notes there is no route. How can this be alleviated?

Upvotes: 0

Views: 44

Answers (1)

Anon
Anon

Reputation: 61

By default, dynamic segments don't accept dots - this is because the dot is used as a separator for formatted routes. If you need to use a dot within a dynamic segment, add a constraint that overrides this.

Go this way:

get '/mainSearch/:latitude/:longitude',
  to: 'requests#mainSearch',
  constraints: { latitude: /\-?\d+\.\d+/, longitude: /\-?\d+\.\d+/ }

Upvotes: 2

Related Questions