Reputation: 527
So what I am trying to do is generate a list of breweries based on a location that the user enters into an input field. How I have it set up is in my javascript I have the browser send ajax requests to the server, the server then communicates with a 3rd party's database and back to the browser.
This code works when I hardcode a location like "Chicago" in the controller but I want to generalize it so I can pass in whatever location the user inputted. I think this has something to do with the params hash but am uncertain as to how to implement.
routes.rb
get 'search' => 'home#search_breweries_by_location'
home controller.rb
def search_breweries_by_location
breweries = @brewery_db.locations.all(locality: location)
render({:json=>breweries})
end
/home/index.html.erb
var location = document.getElementById('location').value;
$.post('/search', {locality: location}, function(data){
for(var i = 0; i < data.length; i++){
$('#brewery-list').append(data[i].brewery.name);
}
});
Upvotes: 0
Views: 74
Reputation: 13354
One of your issues is that you are making a POST
to /search
but you only have a route that matches GET
on /search
. I would start by switching your route to match on POST
instead of GET
.
Upvotes: 0
Reputation: 36
I'm sort of new to Rails myself, but I've always used the following paradigm to look things up in ActiveRecord:
@brewery_db.locations.where(locality: params[:locality])
Again not sure if this solves your issue, but maybe the params[:locality]
part will get you what you need.
Upvotes: 2