cephas
April 10, 2007, 8:41pm
#1
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 # works!
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!
#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
cephas
April 11, 2007, 1:34am
#2
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
Sent from the RubyOnRails Users mailing list archive at Nabble.com .
cephas
April 12, 2007, 9:44pm
#3
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 <- no decimal points
works
renders page withâŚ
Township: 6
Range: 4
Section: 15
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
cephas
April 13, 2007, 12:36am
#4
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 <- 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
Sent from the RubyOnRails Users mailing list archive at Nabble.com .
cephas
April 13, 2007, 3:26pm
#5
On Apr 12, 12:42 pm, âPeteâ [email protected] 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://groups.google.com/group/rubyonrails-talk/browse_frm/thread/6e8c2c7c9f5c69a6/#
cephas
April 13, 2007, 10:28pm
#6
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