I have a controller called Welcome with a view called index which contains a small form where user can enter longitude , latitude values in a text field.
Here is the code in index.html.erb
<%= form_for :welcome do |f| %>
<%= f.label :longitude %><br>
<%= f.text_field :longitude %>
<br>
<br>
<%= f.label :latitude %><br>
<%= f.text_field :latitude %>
<p>
<%= f.submit %>
</p>
<% end %>
Once the user enters the longitude and latitude value i try to capture them in the welcome controller. Here is the code for that.
class WelcomeController < ApplicationController
def index
@long = params[:longitude]
@lat = params[:latitude]
end
end
Following is my routes file
Rails.application.routes.draw do
get 'welcome/index'
post 'welcome/index'
end
I have a table called stop. Inside it there is pre populated data for a stop including a id, name, longitude and a latitude.
What i want to achieve So basically i want to do a small check where i do a query to find the stop id of a stop that has the matching longitude and latitude values entered by the user. Finally I would like to print the that stop id on my index.html.erb page
The issue i am facing So for example in the small form i created. Say i type in value "123" for longitude field and "456" for latitude field. After i click submit I see the following in my terminal
Parameters: {"utf8"=>"✓", "authenticity_token"=>"N7RssIp7sQggxZ2RArEf9CnrUYXK6aG7Ix1MJlI5MoEPDocATHjXdlo24w0gFCTG+4B43ks0sVx1XNlS3RO84Q==", "welcome"=>{"longitude"=>"123", "latitude"=>"456"}, "commit"=>"Save Welcome"}
Rendered welcome/index.html.erb within layouts/application (33.6ms)
Completed 200 OK in 108ms (Views: 107.5ms | ActiveRecord: 0.0ms)
However in my welcome controller when i do the following
Stop.find_by longitude: '@long'
I see this in terminal
Stop Load (0.2ms) SELECT `stops`.* FROM `stops` WHERE `stops`.`longitude` = 0 LIMIT 1
From this it is obvious i am not capturing the user inputs correctly. Can someone please tell me how to fix this?
I know there are better ways of getting locations etc but i am just learning rails and it is vital i understand the basics of doing such things. Please let me know if anything is unclear. I have tried to resolve this for past 2 days without luck i would highly appreciate answer to fix this.