##環境
ruby 2.1.0
Rails 4.1.0
ransack 1.7.0
##Ransackでf.time_select
gemRansack
でお店の開店時間を指定できる検索を実装したく、f.time_select
を用いています。
f.time_select
で時間を指定せずに検索した場合、空欄の場合は全ての結果が返されるようにしたいです。
Item Load (1.5ms) SELECT DISTINCT items
.* FROM items
WHERE items
.time
= '0001-01-01 00:00:00' ORDER BY items
.time1
ASC
となりますが```0001-01-01 00:00:00```のデータ無いため何も返されません。 ```item_controller.rb def index @q = Shop.search(params[:q]) @q.sorts = 'name asc' if @q.sorts.empty? @shops = @q.result(distinct: true) respond_with(@shops) end
item/index.html.erb
1<h1>商品一覧</h1> 2 3<%= search_form_for @q do |f| %> 4 <%= f.label :name_cont, "商品名" %> 5 <%= f.search_field :name_cont %> 6 7 <%= f.label :city, "市町村" %> 8 <%= f.search_field :shop_city_cont %> 9 10 <%= f.label :open_time, "開店時間" %> 11 <%= f.time_select :open_time_eq, :minute_step => 10, :include_blank => true %> 12 <%= f.submit '検索'%> 13<% end %> 14 15<table> 16 <thead> 17 <tr> 18 <th><%= sort_link(@q,:shop_name,'店名') %></th> 19 <th><%= sort_link(@q,:shop_city, '市町村') %></th> 20 <th><%= sort_link(@q,:name,'商品名') %></th> 21 <th><%= sort_link(@q,:price,'値段') %></th> 22 <th><%= sort_link(@q,:open_time,'開店時間') %></th> 23 <th>Content</th> 24 <th colspan="3"></th> 25 </tr> 26 </thead> 27 28 <%= render @items %>
<%= f.time_select :open_time_eq, :minute_step => 10, :include_blank => true %>
ここで開店時間を指定した場合は、時間に一致するものが結果に出ますが、空欄の状態で検索した場合は0時0分に設定されて何も返されずにいます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。