I have a Listing model in my rails app. I have atable with all the listings and a listing_controller class. What I want to do is invoke a method I wrote in the listing_controller. Here is this method:
class ListingsController < ApplicationController
def around
lat = params[:latitude]
long = params[:longitude]
@surroundings = Listing.where("latitude = :lat and longitude = :long", :lat lat, :long long)
end
end
Here is my around.html.erb file
<h1>Surroundings</h1>
<%=@surroundings.inspect%>
<br/>
<%= debug(params) if Rails.env.development? %>
<br/>
Now my listings table has 2 columns: latitude and longitude both string types.
I would like to test the “around” method that I wrote
Here is my routes.rb file
Businesses::Application.routes.draw do
resources :listings
root to: 'listings#index', as: 'listings'
match ':controller(/:action(/:id))(.:format)'
end
Now my understanding is this in order to invoke a method of a controller I need to construct a url and append the method to be invoked after listings like so….
http://localhost:3000/listings/around
but Im failing to understand how will I give the 2 parameters (latitude and longitude) to this method?? Where and how in the url can I add them..
please help
I guess you are talking about regular http get parameters. You can just add them to your URL beginning in the style of
?param1=value1¶m2=value2&....In your example, you would write something like
http://localhost:3000/listings/around?latitude=3.455&longitude=15.2228.To generate such a URL using the Rails helper, the syntax should be something like
listings_around_path(:latitude => "3.455", :longitude => "15.2228").