Having a routing issue, should be really simple but seems straightforward and just not working:
match '/api/get-locations-by-distance/:latitude/:longitude' => 'api#get_locations_by_distance'
with this call:
http://localhost:3000/api/get-locations-by-distance/34.035645/-118.233434
thx for any ideas
edit #1
If I update it to the following:
match '/api/get-locations-by-distance/:latitude/:longitude/:stub' => 'api#get_locations_by_distance'
and
http://localhost:3000/api/get-locations-by-distance/34.035645/-118.233434/stub
It still doesn't work.
edit #2
I tried adding the :format => false but this doens't seem to help either.
match '/api/get-locations-by-distance/:latitude/:longitude' => 'api#get_locations_by_distance', :format => false
with
http://localhost:3000/api/get-locations-by-distance/34.035645/-118.233434
It thinks that .233434 is format
It's like Mik_Die said - the . is the delimiter for the format in Rails.
Here is a work-around: you can specify your own segmentation constraint.
match '/api/get-locations-by-distance/:latitude/:longitude' => 'api#get_locations_by_distance' ,
:constraints => { :latitude => /\d+\.\d+/ , :longitude => /\d+\.\d+/ }
See also:
http://x3ro.de/rails-3-routing-parameters-dots/
Related
I have a route in my routes.rb ie
match '/googleplus' => redirect("https://plus.google.com/+username"), :as => :googleplus
So in browser if someone open
www.example.com/googleplus
he will redirected to google plus page. Now I want to make it
www.example.com/google+
or
www.example.com/+google
to achieve the same thing. How can I do that?
You can't 'simply' do this, you must URL encode it but in this way this will not be accomplished anyway.
Explanation here
You can escape the + character. Your route would look like this.
match '/googleplus' => redirect("https://plus.google.com/\+username"), :as => :googleplus
You'll need to encode the '+', since that usually gets replaced with a space by most browsers. The URI encoding for '+' is '%2B':
match '/google%2B' => redirect("https://plus.google.com/+username"), :as => :googleplus
match '/%2Bgoogle' => redirect("https://plus.google.com/+username"), :as => :googleplus
match "/myroute*" => redirect("http://google.com"), :as => :myroute
The line above in routes.rb is causing the following error
/Users/user/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/racc/parser.rb:349:in `on_error': (Racc::ParseError)
parse error on value ")" (RPAREN)
from /Users/user/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/racc/parser.rb:99:in `_racc_do_parse_c'
from /Users/user/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/racc/parser.rb:99:in `do_parse'
Looks like it is because I'm adding a wildcard (*). Any idea how to solve this?
Wildcard components need to have a "label" as well, e.g.
match "/myroute*something" => redirect("http://google.com"), :as => :myroute
will match /myrouteblah and /myroute/hello/world where params[:something] is blah and /hello/world respectively.
EDIT: Check out http://guides.rubyonrails.org/v3.2/routing.html#route-globbing if you haven't already.
Try this:
match ':redirect' => redirect("http://google.com"), :as => :myroute , :constraints => { :redirect => /myroute.?/i }
I would like to do just a little bit of extra logic in rotues.rb, that probably doesn't belong there, but it seems to make the most sense to me.
I have two conflicting routes. To be primitive:
match '/videos/:browseby' => 'videos#browse', :as => "browse_by"
Where :browseby is looking for a string, such as "Tags", to browse videos by tags.
However, (and most probably saw this coming) I also have my basic show resource (again in primitive form):
match '/videos/:id' => 'videos#show', :as => "video"
Where :id is looking for the integer for the video ID.
Is there a way to add a small bit of logic such as...
match '/videos/:id' => 'videos#show', :as => "video", :format(:id) => :integer
(Which is my hypothetical rails syntax, to help show what I'm looking for.)
I know I can munch this in the Controller level, but it makes more sense to me to handle it at the route level.
You could try using :constraints and a regex:
match '/videos/:id' => 'videos#show', :as => "video", :constraints => { :id => /\d/ }
match '/videos/:browseby' => 'videos#browse', :as => "browse_by"
You'll also want to make sure the looser :browseby version comes after the :id version. Note that regex constraints are implicitly anchored at the beginning so that would work as long as your :browseby values didn't start with a number.
If you have tags that do start with numbers then you could use an object for the constraint and then you could include anchors in your regex:
class VideoIdsOnly
def matches?(request)
request.path =~ %r{\A/videos/\d+\z}
end
end
match '/videos/:id' => 'video#show', :as => "video", :constraints => VideoIdsOnly.new
match '/videos/:browseby' => 'videos#browse', :as => "browse_by"
I'd like to create a route in my rails app to handle a gps-coordinate parameter. The intention is to find restaurants near the given position.
This is were I started:
match "/restaurants/near/:lat/:lng(/:range)", :to => "restaurants#near", :as => "near", :constraints => {:range => /\d+/}
It seems the router has problems with float parameters, an url like /restaurants/near/53.0123/10.5678 isn't recognized. Do you have a solution or best practice for handling GPS coordinates in rails urls?
Thank you!
The problem is caused because Rails try to use the "dots" for search for the format (.:format)
So, you can add some constraints to fix it, for example:
match "/restaurants/near/:lat/:lng(/:range)", :to => "restaurants#near", :as => "near", :constraints => {:lat => /\-?\d+(.\d+)?/, :lng => /\-?\d+(.\d+)?/ , :range => /\d+/}
I've a controller :platform here.
I'm trying to do something like:
/:platform_name/ to redirect to its show, with the parameter. Here is what I've got:
map.resource :platform,
:as => ':platform_name',
:platform_name => /pc|ps2|ps3|wii|ds|psp|xbox-360/
It's working fine. I've other neasted resources to it, and all them are accessing. But.
The problem is, I've only those platform names, but when it doesnt fine another route, it aways fall on this.
if I try /whatever/, it will look for the platform_name => whatever.
I was expecting it to fall into the map.connect ':controller/:action/:id' rule.
When I did :platform_name => /pc|ps2|ps3|wii|ds|psp|xbox-360/, wasnt expected that this rule only apply when the regular expression is fit?
how could i restrict this?
Try changing:
/pc|ps2|ps3|wii|ds|psp|xbox-360/
to
/^(pc|ps2|ps3|wii|ds|psp|xbox-360)$/
Maybe you could try
:requirements => { :platform_name => /pc|ps2|ps3|wii|ds|psp|xbox-360/ }
or
:conditions => { ... }
ActionController::Resources#resources