简体   繁体   English

Rails Ajax搜索未执行任何操作-404错误

[英]Rails Ajax search not doing anything — 404 error

Trying to figure out Ajax search in a Rails 3 app, following this post , which itself borrows from a Railscast. 这篇博文之后 ,尝试在Rails 3应用程序中查找Ajax搜索,该博文本身是从Railscast借来的。 Currently, when I submit, nothing happens, and if I go into the Chrome console, I see this error: 目前,当我提交时,什么也没有发生,如果我进入Chrome控制台,则会看到此错误:

GET http://localhost:3000/search?utf8=%E2%9C%93&search=&_=1361491523102 404 (Not Found)

posts.js line 5 seems to be culpable, but that just seems to be the jQuery call, so I'm not sure what I'm doing wrong. posts.js的第5行似乎很容易受骗,但这似乎只是jQuery调用,所以我不确定自己在做什么错。

My relevant code: 我的相关代码:

posts.js.coffee posts.js.coffee

jQuery ->
    # Ajax search on submit
    $('.search_form').submit( ->
        $.get(this.action, $(this).serialize(), null, 'script')
        false
  )

posts_controller.rb posts_controller.rb

def show
  @search = Post.search(params[:search])

  respond_to do |format|
    format.html # show.html.erb
    format.json { redirect_to @post }
  end
end

def search
  @search = Post.search(params[:search])
  render json: { results: @search }
end

post.rb post.rb

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

show.html.erb show.html.erb

<%= form_tag search_path, method: "get", class: "search_form form-inline",
 style: "display:inline-block;" do %>
    <%= text_field_tag :search, params[:search], placeholder: "Search:", id: "search_field" %>
    <%= submit_tag("Search", name: nil, class: "btn search", remote: true) %>
<% end %>

# Testing for @search stops it from pulling a "no method '.first' for nil NilClass" error.
<div id="list"><%= render partial: 'list', locals: { search: @search } if @search%></div>

list.html.erb list.html.erb

<ul>
    <% @search.each do |p| %>
        <li><a href=<%= p.id %> ><%= p.name %> <span class="manual_small">(<%= p.created_at.strftime("%b %d, %Y") %>)</span></a></li>
    <% end %>
</ul>

Any idea what's going wrong here? 知道这里出了什么问题吗?

EDIT -- Adding relevant lines of routes file: 编辑-添加路线文件的相关行:

resources :posts

# ...

match '/search', to: 'posts#search'

I already had that '/search' route there. 我已经在那儿找到了“ /搜索”路线。 Is that right -- in which case, is the problem somewhere else? 是这样-在这种情况下,问题出在其他地方吗? Does the rest of the code look goodish? 其余代码看起来不错吗?

EDIT -- added search method to PostsController, per Damien's rec. 编辑-根据Damien的记录将搜索方法添加到PostsController中。 New things of interest are a) the way I call the partial, the new search action, and that fact that then I tried to replace the contents of the show method in Post.rb with "Post.all", it still didn't show anything. 令人感兴趣的新事物是:a)我调用部分搜索,新搜索操作的方式,然后我尝试将Post.rb中show方法的内容替换为“ Post.all”的事实,但仍然没有显示任何东西。 No errors thrown, though. 但是,不会抛出任何错误。

"No search action! Didn't realize I needed one. What should be in it? When I tried an earlier version of this without AJAX, I had a search action that was basically a replica of show (+ @search etc), with all the same variables, otherwise it wouldn't work, because it rendered the show page. Is that the right idea? It seemed very unDRY" “没有搜索动作!没意识到我需要一个。它应该包含什么?当我尝试使用AJAX的较早版本时,我的搜索动作基本上是show的副本(+ @search等),带有所有相同的变量,否则它将不起作用,因为它呈现了显示页面。这是正确的主意吗?它看起来很干燥。

Using AJAX, there is no need to render the entire show page. 使用AJAX,无需渲染整个show页面。 Just return the filtered results in desired format: 只需以所需格式返回过滤的结果:

def search
  @search = Post.search(params[:search])
  render json: { results: @search }
end

Add format based returns if necessary. 如有必要,添加基于格式的退货。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM