rails 6.0.2
ubuntu (WSL)
gem 'ransack'
Don't forget bundle
.
It is assumed that post_controller
has already been created.
posts_controller.rb
def index
unless params[:q].blank?
#Split the entered word with a space
split_keyword = params[:q][:content_cont].split(/\p{blank}/)
end
@q = Post.ransack(content_cont_any: split_keyword)
@posts = @q.result
end
Check if there is an input (search word). I think it would be an error without this.
unless params[:q].blnak?
posts/index.html.slim
= search_form_for @q, class: 'mb-5 search-container' do |f|
.form-group.row
= f.search_field :content_cont, placeholder: "Please enter a keyword", class: "form-control"
= f.submit class: "btn btn-outline-primary"
Recommended Posts