banditKing
banditKing

Reputation: 9579

how to add params in Http request to invoke a method on a controller in a rails app

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>


<%[email protected]%>


<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

Upvotes: 1

Views: 730

Answers (2)

Pascal
Pascal

Reputation: 2747

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&param2=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").

Upvotes: 0

Bradford
Bradford

Reputation: 4193

Please see Ruby on Rails Guides: Action Controller Overview and read the section on parameters. You can provide these parameters through the query string parameters of the URL. For example http://localhost:3000/listings/around?latitude=1&longitude=2. You can also POST this data.

Upvotes: 0

Related Questions