railscasts学习笔记(5-2)

一个简单的搜索功能
routes:

  get "search" => 'articles#search'

view:

<%= form_tag('/search',method: :get,enforce_utf8: false) do -%>
  <div><%= search_field_tag "search" %></div>
  <div><%= submit_tag '搜索' %></div>
<% end -%>

cotroller:

  def search
    #render plain: params
    @articles = Article.search(params[:search])
  end

model:

  def self.search(search)
    where("title LIKE ? OR text LIKE ?", "%#{search}%","%#{search}%")
  end
    原文作者:天赢金创
    原文地址: https://segmentfault.com/a/1190000000490399
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞