【问题标题】:Rails 3 Ajax SearchRails 3 Ajax 搜索
【发布时间】:2012-06-06 05:23:00
【问题描述】:

我正在关注 railscasts rails ajax 教程并遇到了一些麻烦,我是否遗漏了什么或者该教程仅适用于 pre rails 3.1?

控制器:

def index

    @notes = Note.search(params[:search])


end

型号:

class Note < ActiveRecord::Base

    def self.search(search)
      if search
        where('name LIKE ?', "%#{search}%")
      else
        scoped
      end
    end


end

查看:

<%= form_tag notes_path, :method => 'get', :id => "notes_search" do %>
  <p>
    <%= text_field_tag :search, params[:search] %>
    <%= submit_tag "Search", :name => nil %>
  </p>
  <div id="notes"><%= render 'notes' %></div>
<% end %>

咖啡文件:

jQuery ->
  # Ajax sorting and pagination on click
  $('#notes td.sortable a, #notes .pagination a').live('click', ->
    $.getScript(this.href)
    false
  )
  # Ajax search on submit
  $('#notes_search').submit( ->
    $.get(this.action, $(this).serialize(), null, 'script')
    false
  )
  # Ajax search on keyup
  $('#products_search input').keyup( ->
    $.get($("#notes_search").attr("action"), $("#notes_search").serialize(), null, 'script')
    false
  )

错误出现在这一行:

ActionView::MissingTemplate in Notes#index

   <div id="notes"><%= render 'notes' %></div>

【问题讨论】:

    标签: javascript ruby-on-rails ruby-on-rails-3


    【解决方案1】:

    ActionView::MissingTemplate 异常基本上表示您尝试渲染的操作没有视图文件。

    您需要有一个名为“_notes.html.erb”的局部视图才能工作。在视图中你应该有这样的东西:

    <%= hidden_field_tag :direction, params[:direction] %>
    <%= hidden_field_tag :sort, params[:sort] %>
    <%= will_paginate @notes %>
    

    我从您所指的教程中获取了该代码http://railscasts.com/episodes/240-search-sort-paginate-with-ajax,也许您还没有这些参数或尚未安装 will_paginate gem,请根据您的需要进行调整。

    【讨论】:

    • 嗨,谢谢你,但它仍然不起作用,只是在页面上获取另一个笔记列表。
    • 你在什么时候得到这个错误?页面加载时还是搜索完成后?
    • 页面加载时。我的控制器中是否缺少返回参数?可能返回 JSON?
    • 没有。错误的原因是render 'notes' 运算符。它基本上说它是为了渲染动作notes,你没有那个。
    • 他们在 products.js.erb 文件中有这个?? $("#products").html("");这是 3.1 之前的版本吗?
    【解决方案2】:

    这与教程完全无关,但你为什么不考虑使用datatables。与 railscasts 教程做同样的事情,也有完整的文档和 API 以使其完全可定制。

    【讨论】:

    • 我不是很喜欢查表,我只需要学习ajax搜索。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    相关资源
    最近更新 更多