0
我正在嘗試做一個簡單的搜索功能。這是模塊中的功能。我有兩列:標題和說明。但是我得到一個錯誤。我不需要在那裏選擇「標題」。在導軌中使用搜索功能的問題
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%')
UPDATE:
這是我的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 %>
:HTTP:/ /stackoverflow.com/questions/7911014/activerecord-find-and-only-return-selected-columns – Trompa