Rails routing send string with dots and int - ruby-on-rails

I'm new to rails...
I try to send such string as :search param: "55.675155, 21.833466" and 2 as :id param...
But something is bad...
i get No route matches [GET] "/exchanger_lists/get_exchangers_via_coordinates/.....
My route file:
match 'exchanger_lists/get_exchangers_via_coordinates/:search,:id' => 'exchanger_lists#get_exchangers_via_coordinates'
But also how then url must look in browser???
How to do this in Rails way? I read doc's, but something is not clear on 100% (
Just how to configure my route and how to call from browser?

match 'exchanger_lists/get_exchangers_via_coordinates/:search,:id' => 'exchanger_lists#get_exchangers_via_coordinates',
constraints: { search: /[^\/]+/ }
From here

You could change your routes like following:
match 'exchanger_lists/get_exchangers_via_coordinates/:x/:y/:id' => 'exchanger_lists#get_exchangers_via_coordinates'
params[:x] and params[:y] will hold your coordinates. I think this is more beautiful code, than holding the to coordinates in one param.

Related

Ruby - A request route with a wildcard doesn't work

Could someone suggest why these 2 routes aren't the same:
get('/:id/' => 'outlets/play#show', :as => :listen, constraints: { id: /thetrack-a123-bay7623/ } )
get('/:id/' => 'outlets/play#show', :as => :listen, constraints: { id: /thetrack-.*/ } )
What I'm trying to achieve is only want that route outlets/play#show to be used when there is an :id that begins with thetrack.
I've found that if I explicitly use those characters its fine i.e. without thetrack in the route it doesnt use that route. However if I use thetrack-.* it still goes into the outlets/play#show route despite thetrack not being present in my request.
Any ideas?
I've tried other regex patterns e.g.
thetrack-.+
thetrack-.+-.+
thetrack-.*-.*
with no luck
If what you are trying to do is route any request /:id/ with ID starting with thetrack- to outlets/play#show, then your configured route should work:
get '/:id/', to: 'outlets/play#show', as: :listen, constraints: { id: /thetrack-.*/ }
Here are some example paths that will route to outlets/play#show using this wildcard:
/thetrack-
/thetrack-a123
/thetrack-a123-bay7623
/:id/ is quite broad reaching. Check that you have not got any other conflicting routes. i.e. another route at root level /:something/ that could be catching the other requests where thetrack- is not specified.

Route with multiple paramaters

I have the route:
match "/invite/create/:first_name/:last_name/:email/:phone" => 'invite#create'
and when I try:
http://localhost:3000/invite/create/bill/bob/bob#gmail.com/1234567890
I get a No route matches [GET] "/invite/create/bill/bob/bob#gmail.com/1234567890" error.
Whats wrong with my route?
If you have other routes that start with the same url fx /invite, then is it important that you define match "/invite/create/:first_name/:last_name/:email/:phone" => 'invite#create' first.
However this is not a very good practice, you should always do a post request when creating new records.

How to create rails route with constrants for numbers-only that accepts empty parameter?

I have created some route like this:
get 'foo/:offset' => 'foo#action', :as => :foo, :constraint => { id: /\d+/ }
It works fine, but: I want rails to route /foo to foo#action if no parameter specified, so not only urls like /foo/123 will be routed but simple /foo too.
How can I change constraint for this? Thanks for help!
I don't know if this is the best solution, but it's a simple one:
Create a route for /foo, and another for /foo/:offset to the same controller and action!

Escaping elements in a URI Ruby On Rails

I have a rails app that I am trying to do a get request with co-ordinates in...
I have a route in my routes.rb like this:
map.connect 'feeds/get/:location', :controller => "feeds", :action => "get"
I can send a string consisting of alphanumeric characters fine, but I need to send co-ordinates in a string in the URI as a get request:
51.896834,0.878906.
So, I escaped the string like so, and append it to my URI.
http://thisisnottheurl.net/feeds/get/51%2E896834%2C0%2E878906.xml
however it looks like rails automatically unescapes the string before the controller and gives me this routing error in the log:
ActionController::RoutingError (No route matches "/feeds/get/51.896834,0.878906.xml" with {:method=>:get}):
How do I stop rails escaping this string (with routes?) so that it can be read in the controller?
I looked at using the match function in routes.rb with regex, but that is rails 3 only...
The only real way I can think of doing this would be as follows, give the route a name as follows:
map.connect 'feeds/get', :controller => "feeds", :action => "get", as: 'get_feeds'
Then you would have a named route helper get_feeds_path, which you could then pass in location and a format as follows:
get_feeds_path(:location => '51.896834,0.878906', :format => 'xml')
What might be an even better idea however, is if you passed in two params, one for each of the coordinates.
get_feeds_path(:x_location => '51.896834', :y_location => '0.878906', :format => 'xml')
Then, the params hash passed to the controller should have a params[:x_location] and a params[:y_location] which you can manipulate to your liking.

ROR route with parentheses in constraint

I'm trying to create a rails route for movies (on the root path) that has parentheses containing the movie's year in it.
E.g. Men in black => "/men-in-black-(1997)"
My route is:
resources :movies,
path:'/',
only:[ :index, :list, :show ],
constraints: { id: /[A-Za-z0-9-]+\(\d{4}\)/ }
When I use this route (movie_path(Movie.first)), I get
"ActionController::RoutingError: No route matches: ..."
When I change the route constraint to:
constraints: { id: /[A-Za-z0-9-]+\\\(\d{4}\\\)/ }
the route works when using the url routing helper. However, the route doesn't work for the reverse mapping (e.g. taking "/men-in-black-(1997)" and routing it to the correct action/controller). When I run (from console):
Rails.application.routes.recognize_path("/men-in-black-(1997)")
I get the same routing error:
ActionController::RoutingError: No route matches
The problem seems to be associated to how rails escapes regex's in routing. For escaping with \( the object-to-route map fails, but url-to-route works. But when escaping with \\\( it is the opposite.
Anyone have any tips or experience with this?
As a workaround hack you could try:
constraints: { id: /[A-Za-z0-9-]+(\\\(\d{4}\\\)|\(\d{4}\))/ }
That is, make the constraint accept either, if it accepts one in one case and the other in the other case.
Which is to say: that's weird, I have no idea why Rails would do that or how to fix it ;)
Well I don't have a lot of experience writing regex constraints, but you could always do a wildcard route and then sanitize in the controller.
match 'movies/*movie' => 'movie#action'
This will give you access to a :movie param with all the characters input

Resources