我正在尝试在索引视图中实现一个简单的搜索框,但它给了我这个错误:
SQLite3::SQLException: no such column: name: SELECT "tickets".* FROM "tickets" WHERE (name LIKE '%s%')
我无法弄清楚为什么会这样,我该如何纠正它。以下是我的MVC
查看
<h1>Listing tickets</h1>
<%= form_tag tickets_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag 'Search' %>
</p>
<% end %>
<table>
<tr>
...
</tr>
<% @tickets.each do |ticket| %>
<tr>
<td><%= ticket.caller_name %></td>
<td><%= ticket.called_date %></td>
<td><%= ticket.problem %></td>
...
<% end %>
</table>
模型
class Ticket < ActiveRecord::Base
attr_accessible :called_date, :caller_name, :problem
has_many :logs, :dependent=>:destroy
def self.search(search)
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
end
控制器
class TicketsController < ApplicationController
def index
@tickets = Ticket.all
@tickets = Ticket.search(params[:search])
respond_to do |format|
format.html # index.html.erb
format.json { render json: @tickets }
end
end
答案 0 :(得分:2)
您正在尝试查询故障单表中不存在的列name
。因此,只需将其替换为存在的列caller_name
即可。替换以下行:
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
带
find(:all, :conditions => ['caller_name LIKE ?', "%#{search}%"])