2

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.

2 Answers 2

1

form_for is a model back form, which means that you need to pass it an object of one of your model which has a longitude and latitude parameters. In this case, you don't have that, so you should use form_tag instead:

<%= form_tag("/welcome/index", method: "post") do %>
  <%= label_tag :longitude %><br>
  <%= text_field_tag :longitude %>
  <br>
  <br>
  <%= label_tag :latitude %><br>
  <%= text_field_tag :latitude %>
  <p>
    <%= submit_tag("Submit") %>
  </p>
<% end %>

Now your routes need a little fixing.

Rails.application.routes.draw do
  get 'welcome/index', to: 'welcome#index'
  post 'welcome/index', to: 'welcome#create'
  get 'welcome/new', to: 'welcome#new'
end

You need 2 different pages, the new page to display the inputs for longitude and latitude, and the index to display your stop information. Now your controller should look like this

class WelcomeController < ApplicationController
  # displays the form, so change the name of the form you have now to new.html.erb
  def new
  end

  # the form will pass to this action to perform logic on longitude and latitude
  def create
    longitude = params[:longitude]
    latitude = params[:latitude]
    @stops = Stop.where("longitude = ? AND latitude = ?", longitude, latitude)
    render :index
  end

  # if it renders from the create action, @stops will be available to use here
  def index
  end
end

Now in your index.html.erb file, display the stop ids that are matched with the users input of longitude and latitude

<% if @stops %>
  <% @stops.each do |stop| %>
    <p>
      stop id: #{stop.id}<br>
      latitude: #{stop.latitude}<br>
      longitude: #{stop.longitude}
    </p>
  <% end %>
<% end %>
Sign up to request clarification or add additional context in comments.

11 Comments

This looks very promising i will try it right now. Thank you so much for your efforts !!! God bless you.
So When you do the following @stops = Stop.where("longitude = ? AND latitude = ?", longitude, latitude) . Is @ stops an array?
Yes, it returns an array of stops that has the matching longitude and latitude, whether it finds 0, 1, or more.
Okay that clears it up now for example in my stop table i have name field, longitude field, latitude field and id field. And i want to print any of those fields would i do #{stop.name} or something because at the moment when there is a match it is simply outputting stop.id on the page. So i know the logic is correct just want to print the actual ID now instead of 'stop.id'
No, its rendering the index template. Generally when you post to a controller action, you want to render another template or redirect to another url.
|
0

In your controller try to use params[:welcome][:longitude] instead of params[:longitude]

Also try Stop.find_by longitude: "'#{@long}'" instead of Stop.find_by longitude: '@long'

1 Comment

when i try params[:welcome][:longitude] i get undefined method `[]' for nil:NilClass

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.