我一直试图按照Railscast #240设置一个AJAX实时搜索,但没有运气。我相信我按照教程编写并正确放置了所有内容。我安装了jquery-rails gem并重新启动了服务器。我已将javascript_include_tag
放在<head>
中。然而,搜索完全按照之前的方式做出反应。有什么我想念的吗?
application.html.erb
<%= stylesheet_link_tag "scaffold", "elements", "layout" %>
<%= javascript_include_tag 'application' %>
<%= javascript_include_tag 'jquery' %>
<%= javascript_include_tag 'jquery_ujs' %>
<%= csrf_meta_tag %>
的application.js
$(function() {
$("#tag_search input").keyup(function() {
$.get($("#tag_search").attr("action"), $("#tag_search").serialize(), null, "script");
return false;
});
});
_tag_list.html.erb
<% @tags.each do |tag| %>
<%= button_to "#{tag.name} - #{tag.reports.size}", assign_tag_url(:tag_id => tag.id, :report_id => @report.id), :method => :post %>
<% end %>
show.js.erb
$("#tag_list").html("<%= escape_javascript(render("tag_list")) %>");
show.html.erb
<div id="search_tags">
<%= form_tag @report, :method => :get, :id => "tag_search" do %>
<p>
<%= text_field_tag :search_tags, params[:search_tags], :placeholder => 'search tags...' %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
</div>
<div id="tag_list">
<%= render 'tag_list' %>
</div>
答案 0 :(得分:0)
尝试在'jquery.js'之后加载'application.js'。