我正在尝试使用字符串remedy
从表remedyName
自动填写搜索栏。
以下是\pages\home.html.erb
$(function() {
var availableTags = "<%= @remedies_remedyName%>";
$( "#autoComplete" ).autocomplete({
source: availableTags
});
});
以下是Pages Controller
def home
@remedies = Remedy.all
end
以下是/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>
答案 0 :(得分:1)
您正在将Ruby变量@remedies_remedyName
打印到您的javascript中。看起来您没有定义此实例变量。
var availableTags = "<%= @remedies_remedyName%>";
将您的js代码更新为以下内容:
var availableTags = "<%= raw(@remedies.map(&:remedyName)).html_safe %>";
注意:我假设您的remedyName
表格中有一个名为Remedy
的列。
答案 1 :(得分:0)
尝试以下方法:
var availableTags = <%= @remedies.pluck(:remedyName) %>;