application.html
<%= form_tag("/posts/search", :method => 'get', :class => 'form') do %>
<div class="search-form">
<%= text_field_tag :search, '', class: 'search-input' %>
<%= submit_tag "検索", :name => nil, class: 'search-btn' %>
</div>
<% end %>
posts_controller.rb
def search
@posts = Post.search(params[:search])
end
post.rb
def self.search(search)
if search
Post.where('name LIKE? OR gender LIKE? OR country LIKE? OR job LIKE? OR content LIKE?',
"%#{search}%", "%#{search}%", "%#{search}%", "%#{search}%", "%#{search}%")
else
Post.all
end
end
search.html.erb
<% @posts.each do |post| %>
<div class="img-link">
<a href="<%= "/posts/#{post.id}" %>" class="thumb-link">
<div>
<img src="<%= "/images/#{post.image}" %>" class="thumb-img">
<div class="thumb-name"><%= post.name %></div>
</div>
</a>
</div>
<% end %>