Fergus Morton Fergus Morton - 7 months ago 18
Ruby Question

Display name from array in auto-complete search bar

I am trying to auto-complete a search bar from a table

remedy
with the string
remedyName
.

Here is the JavaScript I have in
\pages\home.html.erb


$(function() {
var availableTags = "<%= @remedies_remedyName%>";

$( "#autoComplete" ).autocomplete({
source: availableTags
});
});


Here is the code in
Pages Controller


def home
@remedies = Remedy.all
end


Here is the html in
/pages/home


<div class="carousel-caption searchbar">
<form class="search" action="/pages/searchremedy" method="get">
<input id="autoComplete" class="searchTerm" name="searchremedy" placeholder=" Search by treatment type, or tell us what you'd like to treat..." />
<input class="searchButton" type="submit" placeholder="Search" />
</form>
</div>

Answer

You are printing out Ruby variable @remedies_remedyName into your javascript. It does not look like you have defined this instance variable.

var availableTags = "<%= @remedies_remedyName%>";

Update your js code to the following:

var availableTags = "<%= raw(@remedies.map(&:remedyName)).html_safe %>";

Note: I am assuming you have column named remedyName in your Remedy table.