Skip to content

Instantly share code, notes, and snippets.

@buyaka
Last active December 12, 2015 02:47
Show Gist options
  • Select an option

  • Save buyaka/bc619b1fa161a61561fd to your computer and use it in GitHub Desktop.

Select an option

Save buyaka/bc619b1fa161a61561fd to your computer and use it in GitHub Desktop.
#--------------- search ----------------
#1 Add search route into route.rb (before resources :articles)
get '/articles/search' => "articles#search"
#2 Add search action into Post controller
def search
@articles = Article.where("title = ?",params[:q])
end
#3 Add search view into app/views
<h1>Search articles</h1>
<%= form_tag("/articles/search", method: "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
<table border=1>
<tr>
<th>Title</th>
<th>Text</th>
</tr>
<% @articles.each do |article| %>
<tr>
<td><%= link_to article.title, article_path(article.id) %></td>
<td><%= article.content %></td>
</tr>
<% end %>
</table>
#TODO
# add filter for other fields
# Article.where("text = ? and title = ?",params[:text],params[:title])
# to add LIKE filter SQL : name like %aa%
# "name LIKE ? OR postal_code like ?", "%#{search}%", "%#{search}%"
#--------------- search ----------------
#--------------- nested resource ----------------
Post, Comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment