在rails中使用搜索功能的问题

时间:2013-07-22 08:22:16

标签: ruby-on-rails-3

我正在尝试做一个简单的搜索功能。这是模块中的功能。我有两列:标题和说明。但是我收到了一个错误。我需要在那里选择“标题”而不是帖子。

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

我得到的错误是:

SQLite3::SQLException: no such column: name: SELECT "posts".* FROM "posts" WHERE (name LIKE '%first%')

更新:

这是我的index.html.erb文件。我基本上使用了一个表单并列出了所有帖子及其内容。如何更改文件以仅显示搜索的项目?最初应列出所有内容。我无法理解如何做到这一点。有什么想法吗?

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

<% @posts.each do |post| %>
  <h2><%= link_to post.title,post %></h2>
  <p><%= post.content %></p>
  <hr />
  <%= link_to "Add a new post", new_post_path %>
<% end %>

1 个答案:

答案 0 :(得分:0)

您的表name中没有列posts我认为您希望搜索title&amp; description请尝试按照

find(:all, 
     :conditions => ['title LIKE ? OR description like ?', "%#{search}%", "%#{search}%"])