Rails 3 Ajax 搜索 [英] Rails 3 Ajax Search

查看:36
本文介绍了Rails 3 Ajax 搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 railscasts rails ajax 教程,但遇到了一些麻烦,我是否遗漏了什么,还是本教程仅适用于 Rails 3.1 之前的版本?

控制器:

def 索引@notes = Note.search(params[:search])结尾

型号:

class Note ActiveRecord::Basedef self.search(搜索)如果搜索where('name LIKE ?', "%#{search}%")别的有范围的结尾结尾结尾

查看:

<%= form_tag notes_path, :method =>'get', :id =>notes_search"做%><p><%= text_field_tag :search, params[:search] %><%= submit_tag "搜索", :name =>零%></p><div id="notes"><%= render 'notes' %></div><%结束%>

咖啡脚本文件:

jQuery -># Ajax 排序和点击分页$('#notes td.sortable a, #notes .pagination a').live('click', ->$.getScript(this.href)错误的)# Ajax 搜索提交$('#notes_search').提交( ->$.get(this.action, $(this).serialize(), null, 'script')错误的)# Ajax 搜索 keyup$('#products_search input').keyup( ->$.get($("#notes_search").attr("action"), $("#notes_search").serialize(), null, 'script')错误的)

错误在这一行:

ActionView::MissingTemplate in Notes#index<div id="notes"><%= render 'notes' %></div>

解决方案

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,根据你的需要进行调整.>

I'm following the railscasts rails ajax tutorial and geting into some trouble, am i missing something or is the tutorial only meant to be covered for pre rails 3.1?

controller:

def index

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


end

Model:

class Note < ActiveRecord::Base

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


end

View:

<%= 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 %>

Coffescript file:

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
  )

Error is on this line:

ActionView::MissingTemplate in Notes#index

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

解决方案

The ActionView::MissingTemplate exception basically says that there is no view file for the action you're trying to render.

You need to have a partial view called "_notes.html.erb" for it to work. Inside the view you should have something like that:

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

I took that code from the tutorial you're referring to http://railscasts.com/episodes/240-search-sort-paginate-with-ajax, maybe you don't have those params or don't have will_paginate gem installed yet, adjust it to your needs.

这篇关于Rails 3 Ajax 搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆