【问题标题】:Search in Rails在 Rails 中搜索
【发布时间】:2012-08-14 04:43:06
【问题描述】:

我有一个“帖子”表,其中包含titlebody 的属性。

posts_controller.rb:

class PostsController < ApplicationController
  def index
    @posts = Post.search(params[:search], params[:id])
  end
end

index.html.erb:

 <%= form_tag posts_path, :method => 'get'  do %>
   <%= text_field_tag :search, params[:search]%>
   <%= submit_tag "Search", :name => nil  %>
 <% end %>

<hr />
<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
  </tr>
 <tr>
  <td><hr></td>
  <td><hr></td>
</tr>
  <% @posts.each do |post| %>

    <tr>
      <td><%= post.title %></td>
      <td><%= post.text %></td>
      <td><%= link_to 'Show', :action => :show, :id => post.id %></td>
      <td><%= link_to 'Edit', :action => :edit, :id => post.id %></td>
      <td><%= link_to 'Destroy', { :action => :destroy, :id => post.id }, :method => :delete, :confirm => 'Are you sure?' %></td>
    </tr>
    <tr>
      <td><hr></td>
      <td><hr></td>
    </tr>
  <% end %>
</table>

和 post.rb

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

当我提交搜索参数时,我收到一条错误消息:

ActiveRecord::StatementInvalid in Posts#index 
SQLite3::SQLException: no such column: name: SELECT "posts".* FROM "posts"  WHERE (name LIKE '%lorem%') 

Extracted source (around line #23):

23:   <% @posts.each do |post| %>

APD:我想按“标题”进行搜索。

【问题讨论】:

  • 如果你想按标题搜索,为什么要使用name 列,这显然不存在?
  • 您应该发表您的评论作为答案,因为这绝对是这里的问题;)
  • 请告诉我我们需要什么 :title => nil in view?

标签: ruby-on-rails search


【解决方案1】:

虽然这不是您问题的直接答案,但这里有一些资源可以帮助您了解搜索并在 Rails 应用程序中实现它。

A Simple search form

Advanced search form

Searching with AJAX

Powerful search functionality with the Sunspot gem

List of the most popular search tools for Ruby

---------------更新----

Elasticsearch 之所以越来越受欢迎,是因为它具有一些现代的功能,例如即时索引。它有一颗名为轮胎的红宝石。绝对值得一看。

Elasticsearch

Tire

---------------更新 2----

轮胎已退役,已被Elasticsearch-ruby取代

【讨论】:

  • @Richard 感谢有关更换轮胎的建议更新。
  • 它被适当地重命名为'retire'
  • “简单的搜索表单”不是对 SQL 注入开放吗?
【解决方案2】:

Elasticsearch 与 gem searchkick

注意:searchkick 也是由轮胎贡献的)让你工作起来更舒服。

更多参考:

SearchKick

【讨论】:

    【解决方案3】:

    对于来到这里并在 Rails 4 中寻找解决方案的人,请尝试以下示例:

    帖子控制器:

    class PostsController < ApplicationController
      def index
        if params[:search]
          @posts = Post.search(params[:search]).order("created_at DESC")
        else
          @posts = Post.all.order('created_at DESC')
        end
      end
    end
    

    帖子模型:

    def self.search(search)
      # Title is for the above case, the OP incorrectly had 'name'
      where("title LIKE ?", "%#{search}%")
    end
    

    除了搜索表单之外,index.html.erb 保持不变:

    <%= form_tag(posts_path, :method => "get", id: "search-form") do %>
      <%= text_field_tag :search, params[:search], placeholder: "Search Posts" %>
      <%= submit_tag "Search" %>
    <% end %>
    

    请注意,您使用的路径、控制器和列可能不同。

    【讨论】:

      猜你喜欢
      • 2014-07-07
      • 1970-01-01
      • 1970-01-01
      • 2010-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多