2016-02-20 90 views
0

我正在实施一个简单的搜索表单。我想要做的是按标题搜索课程。对于课程我的迁移文件 姑娘CreateCourses <的ActiveRecord ::迁移PG :: UndefinedColumn:错误:列“标题”不存在

def change 
    create_table :courses do |t| 
     t.integer :Course_num 
     t.string :Title 
     t.text :Description 
     t.string :Instructor 
     t.integer :Instructor_ID 
     t.date :Start_date 
     t.date :End_date 
     t.boolean :Status 

     t.timestamps null: false 
    end 
    add_index :courses, :Course_num, unique: true 
    end 
end 

这是我的课程模式:

class Course < ActiveRecord::Base 
    def self.search(search) 
    if search 
     self.where("Title LIKE ?", "%#{search}%") 
    else 
     self.all 
    end 
    end 
end 

这是我的控制器的索引部分:

def index 
    @courses = Course.search(params[:search]) 
    end 

这是我的课程索引视图:

<div class="container"> 
    <div class="row col-md-4 col-md-offset-4" style="left: 0%; top: 70px;"> 
<p id="notice"><%= notice %></p> 

<h1>Listing Courses</h1> 
    <%= form_tag courses_path, :method => 'get' do %> 
     <p> 
     <%= text_field_tag :search, params[:search], placeholder: "Search Course" %> 
     <%= submit_tag "Search", :name => nil %> 
     </p> 
    <% end %> 

    <table> 
    <thead> 
    <tr> 
     <th>Course num</th> 
     <th>Title</th> 
     <th>Description</th> 
    </tr> 
    </thead> 

    <tbody> 
    <% @courses.each do |course| %> 
     <tr> 
     <td><%= course.Course_num %></td> 
     <td><%= course.Title %></td> 
     <td><%= course.Description %></td> 
     <td><%= link_to 'Show', course %></td> 
     <td><%= link_to 'Edit', edit_course_path(course) %></td> 
     <td><%= link_to 'Destroy', course, :method => :delete, :class=>:destroy, data: { confirm: 'Are you sure?' }%></td> 
     </tr> 
    <% end %> 
    </tbody> 
</table> 

<br> 

<%= link_to 'New Course', new_course_path %> 

    </div> 
    </div> 

每次我搜索,它总是告诉我说:

PG::UndefinedColumn: ERROR: column "title" does not exist 
LINE 1: SELECT "courses".* FROM "courses" WHERE (Title LIKE '%2%') 
               ^
HINT: Perhaps you meant to reference the column "courses.Title". 
: SELECT "courses".* FROM "courses" WHERE (Title LIKE '%2%') 

我不知道为什么,因为我把“标题”,而不是“标题”的模式。我该怎么办?谢谢!

+0

'self.where( ' “课程”, “标题” 等。?', “%#{}搜索%”)'? – potashin

+0

@potashin你的男人!问题解决了!非常感谢你!!! – DevArenaCN

+0

不客气,我发布了一个答案 – potashin

回答

1

尝试以下操作:

self.where('"courses.Title" LIKE ?', "%#{search}%") 
相关问题