0

I'm trying to implement a simple search box in my index view but it gave me this error:

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

I can't figure out why this is and how do I correct it. Below is my MVC

View

<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>

Model

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

Controller

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
4

1 回答 1

2

您正在尝试查询name门票表中不存在的列。caller_name因此,在您的情况下,只需将其替换为确实存在的列。替换以下行:

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

find(:all, :conditions => ['caller_name LIKE ?', "%#{search}%"])
于 2013-03-26T18:51:50.837 回答