2013-03-26 61 views
0

我想實現我的索引視圖一個簡單的搜索框,但它給了我這個錯誤:軌搜索表單「沒有這樣的列:名稱」錯誤

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

好像你的票模型沒有一個'name'屬性,爲確保您的數據庫沒有一個'上name'列'票'桌。你是否執行了正確的遷移?你有沒有試過你的代碼中的某處:'ticket.name'? – MrYoshiji 2013-03-26 18:51:36

回答

1

你是tryi ng來查詢票據表上不存在的列name。因此,只需將其替換爲存在的列即可,您的情況爲caller_name。替換以下行:

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

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

啊謝謝,它現在有效 – user2189312 2013-03-26 18:54:22

+0

很高興我能幫上忙。 – 2013-03-26 18:56:04