# Passing in lng and lat via URL, integers work but not floats

**URL:** <https://www.ruby-forum.com/t/passing-in-lng-and-lat-via-url-integers-work-but-not-floats/91609>\
**Category:** Rails\
**Created:** [April 10, 2007, 8:41pm UTC](https://www.ruby-forum.com/t/passing-in-lng-and-lat-via-url-integers-work-but-not-floats/91609 "2007-04-10T20:41:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cephas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e0b2c6/32.png) [@cephas](https://www.ruby-forum.com/u/cephas)\
**Post date:** [April 10, 2007, 8:41pm UTC](https://www.ruby-forum.com/t/passing-in-lng-and-lat-via-url-integers-work-but-not-floats/91609/1 "2007-04-10T20:41:01Z")

</div>

Using the following gode I am able to get an response  
when the lng and lat are integers, but not when they are floats.

[http://localhost:3000/section/query/43,-90](http://localhost:3000/section/query/43,-90) # works!

[http://localhost:3000/section/query/43.005,-90.336](http://localhost:3000/section/query/43.005,-90.336) # does not work ☹

How do I pass in lat and lng parameters from the url?

Thanks in advance!

- Pete

#section\_controller.rb  
class SectionController \< ApplicationController

def query  
lat = params[:lat]  
lng = params[:lng]  
s = Section.find\_by\_sql(“SELECT twp, rng, sec FROM sections WHERE  
distance( the\_geom,‘POINT(#  
{lng} #{lat})’) = 0”)  
site = s[0]  
loc = site.attributes  
@twp = loc[“twp”]  
@rng = loc[“rng”]  
@sec = loc[“sec”]  
render :action =\> ‘query’  
end

end

* * *

# section.rb

class Section \< ActiveRecord::Base  
set\_primary\_key “gid”  
end

* * *

# query.rhtml
 Query Results\<%= controller.action\_name %\> \<%= stylesheet\_link\_tag 'scaffold' %\>

\<%= flash[:notice] %\>

Township: \<%= h @twp %\>

Range: \<%= h @rng %\>

Section: \<%= h @sec %\>

* * *

# routes.rb

ActionController::Routing::Routes.draw do |map|

map.connect ‘:controller/:action/:lat,:lng’

end

* * *

---

<div class="post-metadata">

**Author:** ![cephas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e0b2c6/32.png) [@cephas](https://www.ruby-forum.com/u/cephas)\
**Post date:** [April 11, 2007, 1:34am UTC](https://www.ruby-forum.com/t/passing-in-lng-and-lat-via-url-integers-work-but-not-floats/91609/2 "2007-04-11T01:34:32Z")

</div>

You’ll should escape the string:

link\_to(:action =\> ‘query’, :controller =\> ‘section’, :lat =\>  
url\_encode(lat), :lon =\> url\_encode(lon))

However, it seems the only unsafe character you have is the comma. If  
you  
split into lat and lon as I did above, all should be fine for reassembly  
in  
your controller as:

@whatever = Section.find\_by\_lat\_lon(params[:lat] + ‘,’ params[:lon])  
#assuming method exists 🙂

Pete DeVries-2 wrote:

> ```
> s = Section.find_by_sql("SELECT twp, rng, sec FROM sections WHERE
> 
> ```
> 
> end
> 
> \<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> 
> \<%= flash[:notice] %\>
> 
> ActionController::Routing::Routes.draw do |map|

–  
View this message in context:  
[http://www.nabble.com/Passing-in-lng-and-lat-via-URL%2C-integers-work-but-not-floats-tf3554838.html#a9930745](http://www.nabble.com/Passing-in-lng-and-lat-via-URL%2C-integers-work-but-not-floats-tf3554838.html#a9930745)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![cephas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e0b2c6/32.png) [@cephas](https://www.ruby-forum.com/u/cephas)\
**Post date:** [April 12, 2007, 9:44pm UTC](https://www.ruby-forum.com/t/passing-in-lng-and-lat-via-url-integers-work-but-not-floats/91609/3 "2007-04-12T21:44:05Z")

</div>

Routing Errors with lat,lon. Integers work but not numbers with  
decimals.  
I suspect that something needs to be modified in the routing to deal  
with decimal points.

Any suggestions! Thanks in advance - Pete

Here is the behavior and code…

[http://localhost:3000/table\_points/query/43,-90](http://localhost:3000/table_points/query/43,-90) ← no decimal points  
works

renders page with…

Township: 6  
Range: 4  
Section: 15

[http://localhost:3000/table\_points/query/43.05,-90.33](http://localhost:3000/table_points/query/43.05,-90.33) ← decimals  
points create routing error  
renders page with …

Routing Error

no route found to match “/table\_points/query/43.05,-90.33” with  
{:method=\>:get}

routes.rb

# Install the default route as the lowest priority.

map.connect ‘table\_points/query/:latitude,:longitude’, :controller  
=\> ‘table\_points’, :action =\> ‘query’

table\_points\_controller.rb

def query  
s = Section.find\_by\_sql(“SELECT twp, rng, sec FROM sections WHERE  
distance( the\_geom,‘POINT(#{lon} #{lat})’) = 0”)  
site = s[0]  
loc = site.attributes  
@twp = loc[“twp”]  
@rng = loc[“rng”]  
@sec = loc[“sec”]  
render :action =\> ‘query’  
end

---

<div class="post-metadata">

**Author:** ![cephas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e0b2c6/32.png) [@cephas](https://www.ruby-forum.com/u/cephas)\
**Post date:** [April 13, 2007, 12:36am UTC](https://www.ruby-forum.com/t/passing-in-lng-and-lat-via-url-integers-work-but-not-floats/91609/4 "2007-04-13T00:36:22Z")

</div>

Use url\_encode to clean up your url. It’s probably the comma that’s  
causing  
you grief.

Pete DeVries-2 wrote:

> [http://localhost:3000/table\_points/query/43.05,-90.33](http://localhost:3000/table_points/query/43.05,-90.33) ← decimals
> 
> # Install the default route as the lowest priority.
> 
> ```
> @twp = loc["twp"]
> 
> ```
> 
> > Pete DeVries-2 wrote:
> > 
> > > Thanks in advance!  
> > > distance( the\_geom,'POINT(#
> > 
> > > ```
> > > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> > > 
> > > ```
> 
> >

–  
View this message in context:  
[http://www.nabble.com/Passing-in-lng-and-lat-via-URL%2C-integers-work-but-not-floats-tf3554838.html#a9969772](http://www.nabble.com/Passing-in-lng-and-lat-via-URL%2C-integers-work-but-not-floats-tf3554838.html#a9969772)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![cephas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e0b2c6/32.png) [@cephas](https://www.ruby-forum.com/u/cephas)\
**Post date:** [April 13, 2007, 3:26pm UTC](https://www.ruby-forum.com/t/passing-in-lng-and-lat-via-url-integers-work-but-not-floats/91609/5 "2007-04-13T15:26:33Z")

</div>

On Apr 12, 12:42 pm, “Pete” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Routing Errors with lat,lon. Integers work but not numbers with  
> decimals.  
> I suspect that something needs to be modified in the routing to deal  
> with decimal points.
> 
> Any suggestions! Thanks in advance - Pete

the periods in URLs issue discussed a couple threads down:

[http://skwpspace.com/2007/02/27/rails-12-breaks-url-routing-with-dots/](http://skwpspace.com/2007/02/27/rails-12-breaks-url-routing-with-dots/)  
[http://groups.google.com/group/rubyonrails-talk/browse\_frm/thread/6e8c2c7c9f5c69a6/#](http://groups.google.com/group/rubyonrails-talk/browse_frm/thread/6e8c2c7c9f5c69a6/#)

---

<div class="post-metadata">

**Author:** ![cephas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e0b2c6/32.png) [@cephas](https://www.ruby-forum.com/u/cephas)\
**Post date:** [April 13, 2007, 10:28pm UTC](https://www.ruby-forum.com/t/passing-in-lng-and-lat-via-url-integers-work-but-not-floats/91609/6 "2007-04-13T22:28:20Z")

</div>

I tried my code without using decimals by multiplying the longitude  
and latitudes by 1000 and  
the dividing them again once they were passed to the controller. This  
works so it seems  
the decimal point was the problem.

However using the “requirements” in the routes.rb does not seem to fix  
the problem. I still have a routing error, below is the error message  
and my routes.rb

no route found to match “/table\_points/query/43.05,-90.89” with  
{:method=\>:get}

routes.rb

map.connect ‘table\_points/query/:latitude,:longitude’, :controller  
=\> ‘table\_points’, :action =\> ‘query’, :requirements =\> { :latitude  
=\> /._/, :longitude =\> /._/ }

Any suggestions would be greatly appreciated 🙂

Thanks in Advance,

-Pete
