构建全局自动完成搜索时,如何修复搜索栏中未定义的方法“form_with”?
How to fix undefined method `form_with' in my search bar when building out Global Autocomplete Search?
当我尝试 运行 在我的 rails 应用程序中跨多个模型进行全局自动完成搜索并利用 easy-autocomplete jquery 库和搜查 gem 进行搜索。
具体来说,我认为错误出在我的导航栏页面中,因为我在尝试 运行 查询时收到此错误 - undefined method form_with for #<#<class:0x007f875a6f8b58>:0x007f87600e0aa8>
我决定破解并将 form_with 切换为 form_tag,但随后我在 undefined method text_field for nil:NilClass"
下面的行中收到错误,我在下面列出了所有相关代码。非常感谢您的帮助。
Main_Controller.rb
class MainController < ApplicationController
def index
end
def search
@movies = Movie.ransack(name_cont: params[:q]).result(distinct: true)
@directors = Director.ransack(name_cont: params[:q]).result(distinct: true)
respond_to do |format|
format.html {}
format.json {
@movies = @movies.limit(5)
@directors = @directors.limit(5)
}
end
end
end
_navbar.html.erb
<%= form_with url: search_path, local: true, method: :get, html: { class: "form-inline my-2 my-lg-0" } do |form| %>
<%= form.text_field :q, placeholder: "Search", data: { behavior: "autocomplete" }, class: "form-control mr-sm-2" %>
<%= form.button "Search", class: "btn btn-outline-success my-2 my-sm-0" %>
<% end %>
search.html.erb
<div class="row">
<div class="col">
<h1>Movies</h1>
<% @movies.each do |movie| %>
<p><%= link_to movie.name, movie %></p>
<% end %>
</div>
<div class="col">
<% @directors.each do |director| %>
<p><%= link_to director.name, director %></p>
<% end %>
</div>
</div>
search.js
document.addEventListener("turbolinks:load", function() {
$input = $("[data-behavior='autocomplete']")
var options = {
getValue: "name",
url: function(phrase) {
return "/search.json?q=" + phrase;
},
categories: [
{
listLocation: "movies",
header: "<strong>Movies</strong>",
},
{
listLocation: "directors",
header: "<strong>Directors</strong>",
}
],
list: {
onChooseEvent: function() {
var url = $input.getSelectedItemData().url
$input.val("")
Turbolinks.visit(url)
}
}
}
$input.easyAutocomplete(options)
});
在您的 gem 文件中将 rails gem 更改为最新版本:gem 'rails'、'~> 5.1.1' 或更高版本
控制台:捆绑包更新
重新启动您的 rails 应用程序
希望这对你有用
当我尝试 运行 在我的 rails 应用程序中跨多个模型进行全局自动完成搜索并利用 easy-autocomplete jquery 库和搜查 gem 进行搜索。
具体来说,我认为错误出在我的导航栏页面中,因为我在尝试 运行 查询时收到此错误 - undefined method form_with for #<#<class:0x007f875a6f8b58>:0x007f87600e0aa8>
我决定破解并将 form_with 切换为 form_tag,但随后我在 undefined method text_field for nil:NilClass"
下面的行中收到错误,我在下面列出了所有相关代码。非常感谢您的帮助。
Main_Controller.rb
class MainController < ApplicationController
def index
end
def search
@movies = Movie.ransack(name_cont: params[:q]).result(distinct: true)
@directors = Director.ransack(name_cont: params[:q]).result(distinct: true)
respond_to do |format|
format.html {}
format.json {
@movies = @movies.limit(5)
@directors = @directors.limit(5)
}
end
end
end
_navbar.html.erb
<%= form_with url: search_path, local: true, method: :get, html: { class: "form-inline my-2 my-lg-0" } do |form| %>
<%= form.text_field :q, placeholder: "Search", data: { behavior: "autocomplete" }, class: "form-control mr-sm-2" %>
<%= form.button "Search", class: "btn btn-outline-success my-2 my-sm-0" %>
<% end %>
search.html.erb
<div class="row">
<div class="col">
<h1>Movies</h1>
<% @movies.each do |movie| %>
<p><%= link_to movie.name, movie %></p>
<% end %>
</div>
<div class="col">
<% @directors.each do |director| %>
<p><%= link_to director.name, director %></p>
<% end %>
</div>
</div>
search.js
document.addEventListener("turbolinks:load", function() {
$input = $("[data-behavior='autocomplete']")
var options = {
getValue: "name",
url: function(phrase) {
return "/search.json?q=" + phrase;
},
categories: [
{
listLocation: "movies",
header: "<strong>Movies</strong>",
},
{
listLocation: "directors",
header: "<strong>Directors</strong>",
}
],
list: {
onChooseEvent: function() {
var url = $input.getSelectedItemData().url
$input.val("")
Turbolinks.visit(url)
}
}
}
$input.easyAutocomplete(options)
});
在您的 gem 文件中将 rails gem 更改为最新版本:gem 'rails'、'~> 5.1.1' 或更高版本 控制台:捆绑包更新 重新启动您的 rails 应用程序 希望这对你有用