【发布时间】:2012-08-14 04:43:06
【问题描述】:
我有一个“帖子”表,其中包含title 和body 的属性。
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