简体   繁体   English

Rails:需要帮助来构建基本的AJAX搜索表单并显示结果

[英]Rails: Need help building a basic AJAX search form and displaying results

I am trying to build a search form and am having trouble understanding the proper way to use UJS in my specific situation. 我正在尝试构建搜索表单,但在理解特定情况下使用UJS的正确方法时遇到了麻烦。 The main issue I have is that I can't figure out how to take the params selected in my form and execute the query then return the results of the search. 我遇到的主要问题是我无法弄清楚如何获取在表单中选择的参数并执行查询,然后返回搜索结果。

I would like to be able to select several "search criteria" from models I have using dropdown select elements and date fields. 我希望能够从使用下拉选择元素和日期字段的模型中选择几个“搜索条件”。 Upon selecting the search items to build a query I want to submit a POST or GET request and have the results returned and displayed in a list below the search form via ajax without reloading the page. 选择搜索项以构建查询后,我要提交POST或GET请求,并通过ajax将结果返回并显示在搜索表单下方的列表中,而无需重新加载页面。

Currently, I have a static page called search with a route setup as: 当前,我有一个名为search的静态页面,其路由设置为:

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

index.html.erb index.html.erb

<h1>Search</h1>

<!-- search form -->
<div id="search">  
  <%= render 'form' %>
</div>

<!-- search results -->
<div id="results">
</div>

I have a SearchController with an 'index' action that handles loading up all the collections of items to put into my search form dropdown menus built using collection_select() methods. 我有一个带有'index'动作的SearchController,该动作可处理所有项目集合的加载,以放入使用collection_select()方法构建的搜索表单下拉菜单中。

SearchController SearchController

class SearchController < ApplicationController
  def index
      # load up all the items to display as selectable search parameters to build query from
      # Collections, Categories, Names
      @collections = Collection.all
      @categories = Category.all
      @names = Name.all            
  end

  def create
    @collection = Collection.find(params[:collection][:id])
    @category = Category.find(params[:category][:id])
    @name = Name.find(params[:fullname][:id])      

    respond_to do |format|
      format.html { redirect_to search_url }
      format.js
    end    
  end
end

The form I am using in a partial: _form.htm.erb 我在部分表单中使用的表单:_form.htm.erb

<%= form_tag( {controller: "search"}, class: "search_form", remote: true) do %>
  <%= label_tag("Categories: ") %>
  <%= collection_select(:category, :id, @categories, :id, :name, {}, html_options = { multiple: false }) %>

  <%= label_tag("Collections: ") %>
  <%= collection_select(:collection, :id, @collections, :id, :title, {}, html_options = { multiple: false }) %>

  <%= label_tag("Names: ") %>
  <%= collection_select(:name, :id, @names, :id, :fullname, {}, html_options = { multiple: false }) %>

  <%= submit_tag("Submit") %>
<% end %>

When I submit the form in the page I see the ajax request with params in the Chrome console. 当我在页面中提交表单时,我在Chrome控制台中看到带参数的ajax请求。 I tried to give the form_tag an action in the hash but it can't seem to find the route unless I specify it in the routes.rb file. 我试图在哈希中给form_tag一个动作,但是除非我在route.rb文件中指定它,否则它似乎找不到路由。

Ex, 当然,

<%= form_tag( {controller: "search", action: "create"}, class: "search_form", remote: true) do %>

Q: Do I need to have a special route if I am using ajax? 问:如果我使用的是ajax,是否需要特殊的路线?

Q: How do I bring the params into a SearchController action of any name and do something with it? 问:如何将参数带入任何名称的SearchController动作中并对其进行处理?

I would like to first be able to display the search query items as text in the results div so I know how the action works. 我希望首先能够在结果div中将搜索查询项显示为文本,以便我知道该操作的工作原理。 I imagine I would just use js/jQuery to append the values of the params submitted to the results div. 我想我只会使用js / jQuery将参数的值附加到结果div中。

Q: Is there another way to do something like this? 问:还有另一种方法可以做这样的事情吗?

Strongly recommend to go with this approach: Search, Sort, Paginate with AJAX 强烈建议使用以下方法: 使用AJAX进行搜索,排序和分页

by the way the jquery method .live() which author using is outdated and were replaced with .delegate() jquery documentation .deleate() 顺便说一下,作者使用的jquery方法.live()已过时,并已替换为.delegate() jquery文档.deleate()

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

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