表单搜索在 rails 应用中不起作用

form search not working in rails app

在我的 rails 应用程序中尝试实现搜索时,我可以让我的应用程序查询输入的搜索参数,但该应用程序不显示符合查询条件的内容。

你能看出为什么基于下面的代码吗?

index.html.erb:

<div id="search">
  <%= form_tag links_path, method: :get, authentication: :false, id: 'search-form' do %>
  <p>
    <%= text_field_tag :search %>
    <%= submit_tag "Search Party", name: :nil %>
  </p>
  <% end %>
</div>

<div id="link"><%= render @links %></div>

<%= will_paginate @links %>

links_controller.rb:

  def index
    if params[:search]
      Link.where("name LIKE ?", "%#{params[:search]}%")
      # Link.find(:all, :conditions => ["name LIKE ?", "%#{params[:search]}%"])
    else 
      Link.all
    end

      ordered_links = Link.order(cached_votes_score: :desc)
      @links = ordered_links.paginate(page: params[:page], per_page: 20)

      respond_to do |format| 
        format.js
        format.html
      end
  end

处理的URL:

http://localhost:3000/links?utf8=%E2%9C%93&search=97&nil=Search+Party

Rails 日志:

Started GET "/links?utf8=%E2%9C%93&search=97&nil=Search+Party" for ::1 at 2016-01-04 20:43:32 -0500
Processing by LinksController#index as HTML
  Parameters: {"utf8"=>"✓", "search"=>"97", "nil"=>"Search Party"}
  Link Load (0.3ms)  SELECT  "links".* FROM "links"  ORDER BY "links"."cached_votes_score" DESC LIMIT 20 OFFSET 0

如果您对我的部分链接的外观感到好奇,我将其包括在内。

_links.html.erb:

<div class="link row clearfix">
    <h2>
      <%= image_tag link.avatar.thumb %>
    </h2>
    <h2>
      <%= link_to link.title, link %><br>
    </h2>
    <p>
      <%= link_to link.url, link %><br>
    </p>

<!-- acts_as_votable for like_link -->
    <div class="btn-group">
        <%= link_to like_link_path(link), method: :put, class: "btn btn-default btn-sm" do %>
          <span class="glyphicon glyphicon-chevron-up"></span>
          Upvote
          <%= link.get_upvotes.size %>
        <% end %>
        <%= link_to dislike_link_path(link), method: :put, class: "btn btn-default btn-sm" do %>
          <span class="glyphicon glyphicon-chevron-down">
          Downvote
          <%= link.get_downvotes.size %>
        <% end %>
    </div>
</div>

SQL Yevgeniy 建议编辑后出现错误:

ActionView::Template::Error (SQLite3::SQLException: no such column: name: SELECT  "links".* FROM "links" WHERE (name LIKE '%98%')  ORDER BY "links"."cached_votes_score" DESC LIMIT 20 OFFSET 0):
     7:   <% end %>
     8: </div>
     9: 
    10: <div id="link"><%= render @links %></div>
    11: 
    12: <%= will_paginate @links %>
  app/views/links/index.html.erb:10:in `_app_views_links_index_html_erb__2780104518371497637_70253166090580'
  app/controllers/links_controller.rb:7:in `index'

虽然我有匹配 98 的内容,但控制器似乎没有选择搜索的项目,而是移动到 if 语句的 'else' 部分。

控制器正在获取标准,只是在 if 块之后你正在做:

  ordered_links = Link.order(cached_votes_score: :desc)
  @links = ordered_links.paginate(page: params[:page], per_page: 20)

它从 link table 中选择所有内容并直接订购。你之前所做的任何事情都没有效果,因为它没有分配给变量。

在您的 links_controller.rb 中试试这个:

  def index
      @links = ordered_links.paginate(page: params[:page], per_page: 20)

      respond_to do |format| 
        format.js
        format.html
      end
  end

  private

    def search_criteria_present?
       !params[:search].nil?
    end

    def searched_links
      if search_criteria_present?
        Link.where("name LIKE ?", "%#{params[:search]}%")
      else
        Link
      end
    end

    def ordered_links
      searched_links.order(cached_votes_score: :desc)
    end