简体   繁体   English

SQLite3 :: SQLException在Rails中实现搜索功能

[英]SQLite3::SQLException Implementing Search Function In Rails

I am working on a search function in Rails. 我正在使用Rails的搜索功能。 I followed the following tutorial 我遵循了以下教程

http://railscasts.com/episodes/37-simple-search-form http://railscasts.com/episodes/37-simple-search-form

I have therefore updated my tutorial.rb with the following 因此,我使用以下内容更新了tutorial.rb

def self.search(search)
  if search
    find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
  else
    find(:all)
  end
end

Updating my tutorials_controller.rb with the following 使用以下内容更新我的tutorials_controller.rb

def index
  @tutorials = Tutorial.search(params[:search])
end

Finally my view with 最后我的看法

<div id="search">
      <%= form_tag tutorials_path, :method => 'get' do %>
    <p>
      <%= text_field_tag :search, params[:search] %>
      <%= submit_tag "Search", :name => nil, :class => "btn btn-success" %>
     </p>
    <% end %>
 </div>

So it is all displaying. 一切都在显示。 When I hit the search box I get the following error. 当我点击搜索框时,出现以下错误。

ActiveRecord::StatementInvalid in TutorialsController#index

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

Any help greatly appreciated. 任何帮助,不胜感激。 Still new to this and a little our of my depth. 仍然是新事物,这是我的深度。 I can see that it is having problem getting information from the database but not sure how to cure it. 我可以看到它在从数据库中获取信息时遇到问题,但不确定如何解决它。

Cheers! 干杯!

As per the error SQLite3::SQLException: no such column: name , there is no column name in the tutorials table. 根据错误SQLite3::SQLException: no such column: nametutorials表中没有列name

If you intended to search the tutorials table based on name field then add it to the tutorials table with a migration: 如果你打算搜索tutorials基于表name字段,然后将其添加到tutorials与迁移表:

rails g migration AddNameToTutorial name

run rake db:migrate 运行rake db:migrate

Insert some records in Tutorial table with names. 将一些带有名称的记录插入Tutorial表中。

Then, go ahead and search based on name field in tutorials table with your current code. 然后,继续使用当前代码根据tutorials表中的名称字段进行search


Second option is, if you intended to search tutorials table based on some other existing field then just update the search method as below: 第二种选择是,如果您打算基于其他existing field搜索tutorials表,则只需更新搜索方法,如下所示:

def self.search(search)
  if search
    find(:all, :conditions => ['fieldname LIKE ?', "%#{search}%"])
  else
    find(:all)
  end
end

where replace fieldname with the attribute name that you want to search on. 其中,将fieldname替换为要搜索的属性名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM