railscasts學習筆記(5-2)

天贏金創發表於2014-05-02

一個簡單的搜尋功能
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