閲覧いただきありがとうございます。
Ransackを使った検索機能の実装を試みているのですが、苦戦しています。
まず、実現したいことは下記2つです。
・データベース内にある複数カラム(title、contents)のすべてからキーワードを検索したい
・キーワード検索結果が表示されない場合は「検索結果がありませんでした。」と表示したい
http://qiita.com/fujitora/items/b2134bf6abcfda79c47f
こちらの記事を参考に進めてみたのですが、検索後のURLは「%5D=東京&commit=検索する」変更されているのですが、実際に検索結果が反映されません。。
開発環境:Ruby on Rails 5.0.0.1
【app/controllers/application_controller.rb】
Ruby
1class ApplicationController < ActionController::Base 2 before_filter :set_search 3 protect_from_forgery with: :exception 4 include SessionsHelper 5 6 def set_search 7 @search = Restaurant.ransack(params[:q]) 8 @ssearch_restaurants = @search.result.paginate(page: params[:page]) 9 end 10end
【app/views/restaurants/search.html.erb】
Ruby
1 <%= search_form_for @search, url: search_restaurants_path do |f| %> 2 <div class="title"> 3 <%= f.text_field :contents_cont, placeholder: "キーワードを検索する", class: "form-control search-result-keyword col-md-6" %> 4 <%= f.submit ("検索する"), class: "btn btn-form col-md-6" %> 5 </div> 6 <% end %>
Ruby
1<% @restaurants.each do |restaurant| %> 2・・・ 3<% end %>
すみませんが、お助けくださいませ。
どうぞよろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/05 01:26