表单搜索无法在rails app中运行

时间:2016-01-05 01:51:33

标签: ruby-on-rails ruby-on-rails-4 search methods params

尝试在我的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

已处理的网址:

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>

来自Yevgeniy的建议编辑后的SQL错误:

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'

似乎控制器没有接收搜索到的项目并且正在转移到if语句的“else”部分,尽管我的内容与98匹配。

1 个答案:

答案 0 :(得分:1)

控制器正在接收标准,就在您正在执行的if阻止之后:

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

link表中选择所有内容,然后对其进行排序。您之前所做的任何事情都没有效果,因为它没有分配给变量。

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