我在此railscast http://railscasts.com/episodes/37-simple-search-form?autoplay=true之后为我的Procedure表设置了搜索机制。我几乎完全遵循它,但当我提交搜索时出现此错误:
ActiveRecord::RecordNotFound at /procedures
Couldn't find all Procedures with 'id': (all, {:conditions=>["name LIKE ?", "%Emergency%"]}) (found 0 results, but was looking for 2)
我想按名称搜索“程序”,这个错误使它看起来像是按ID搜索?
以下是观点:
<%= form_tag procedures_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search], :id => "welcome-search" %>
<%= submit_tag "Search Procedures", :name => nil, :class => "btn btn-success btn-lg" %>
我的控制器:
def index
@procedures = Procedure.search(params[:search])
render `procedures/index`
end
我的模特:
def self.search(search)
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
我很困惑,因为railscast让它看起来很容易上班。谢谢。
答案 0 :(得分:2)
find(:all)
已经很老了,因为rails 3.2已经不再可用了。而是使用where
:
def self.search(search)
if search
where('name LIKE ?', "%#{search}%")
else
all
end
end