2008年8月4日 星期一

Simple Search Form

http://railscasts.com/episodes/37


<!-- projects/index.rhtml -->

<% form_tag projects_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>


# projects_controller.rb
def index
@projects = Project.search(params[:search])
end

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

沒有留言: