###前提・実現したいこと
検索機能を実装したいです。
・フォームへキーワード入力。
・detection.html.erbへ遷移して条件に合致したものを表示
###発生している問題・エラーメッセージ
検索ボタンを押すと、同じページへ遷移してしまいます。
ドメイン/bots?utf8=%E2%9C%93&b_search%5Btitle%5D=%E5%B9%BC%E5%A5%B3%E6%88%A6%E8%A8%98%E3%80%80%E7%AC%AC1%E8%A9%B1%E3%80%8C%E3%83%A9%E3%82%A4%E3%83%B3%E3%81%AE%E6%82%AA%E9%AD%94%E3%80%8D%09&button=
検索フォームはindex.html.erbにあります。
これは、Botモデルのbots_controller.rbのindexメソッドで実現してます。
検索フォームを押して期待していることは、
パスを指定しているdetectionメソッドでdetection.html.erbに遷移することです。
index.html.erb → 検索 → detection.html.erb
###該当のソースコード
○ルーティング
rails routes Prefix Verb URI Pattern Controller#Action root GET / pages#index bots_crawl GET /bots/crawl(.:format) bots#crawl bots_detection GET /bots/detection(.:format) bots#detection detection_bot GET /bots/:id/detection(.:format) bots#detection bot GET /bots/:id(.:format) bots#index bots POST /bots(.:format) bots#create new_bot GET /bots/new(.:format) bots#new edit_bot GET /bots/:id/edit(.:format) bots#edit GET /bots/:id(.:format) bots#show PATCH /bots/:id(.:format) bots#update PUT /bots/:id(.:format) bots#update DELETE /bots/:id(.:format) bots#destroy bots_crawl GET /bots/crawl(.:format) bots#crawl bots_detection GET /bots/detection(.:format) bots#detection★★ detection_bot GET /bots/:id/detection(.:format) bots#detection★入れ子っでも試しましたがダメでした★ bots GET /bots(.:format) bots#index★★ POST /bots(.:format) bots#create new_bot GET /bots/new(.:format) bots#new edit_bot GET /bots/:id/edit(.:format) bots#edit bot GET /bots/:id(.:format) bots#show PATCH /bots/:id(.:format) bots#update PUT /bots/:id(.:format) bots#update DELETE /bots/:id(.:format) bots#destroy
○ビュー
【bots/index.html.erb】
<form class="form-inline col-xs-2" role="form"> <%= form_for(@b_search_form, as: 'b_search', url: bots_detection_path, html: {method: :get}) do |f| %> <%= f.text_field :title, placeholder: "検索", class: :"form-control input-sm" %> <%= button_tag(class: :"form-control input-sm", size: :"10") do %> <%= image_tag('search.png', { :border => '0', :alt => '検索'}) %> <% end %> <% end %> </form>
【bots/detection.html.erb】
<%= link_to "新規登録", new_bot_path %> <%= form_for @b_search_form, as: 'b_search', url: bots_detection_path, html: {method: :get} do |f| %> <%= f.text_field :title, placeholder: "検索", class: :"form-control input-sm", size: :"60" %> <%= button_tag(class: :"form-control input-sm", size: :"10", style: :"margin-top: -14px;") do %> <%= image_tag('search.png', { :border => '0', :alt => '検索'}) %> <% end %> <% end %> <table class="type07"> <thead> <tr> <th scope="cols"></th> <th scope="cols">選挙名</th> <th scope="cols">URL</th> <th scope="cols">記事ID</th> <th scope="cols">開票日</th> <th scope="cols">管理</th> </tr> </thead> <tbody> <% @search_bots.each do |search_bot| %> <tr> <th scope="row"><%= search_bot.id %></th> <td><%= search_bot.name %></td> <td><%= link_to "#{search_bot.url}", search_bot.url %></td> <td><%= search_bot.article_id %></td> <td><%= search_bot.date %></td> <td> <%= link_to "編集", edit_bot_path(search_bot.id) %> <%= link_to "削除", search_bot, method: :delete, data: { confirm: "本当に削除してもいいですか?" } %> </td> </tr> <% end %> </tbody> </table>
○コントローラ
【bots_controller.rb】
def index @b_search_form = SearchForm.new @bots = Bot.all end def detection ##検索結果表示ページを表示 @b_search_form = SearchForm.new(params[:b_search]) @search_bots = @b_search_form.bot_search(@b_search_form.title) end
【app/forms/search_form.rb】
class SearchForm include ActiveModel::Model attr_accessor :title ##部分一致させる def contains(arel_attribute, value) arel_attribute.matches("%#{escape_like(value)}%") end def bot_search(str) Bot.where("title like '%" + str + "%' OR url like '%" + str + "%' OR page_id like '%" + str + "%' OR date like '%" + str + "%'") if title.present? end end
○ログ
★DEPRECATION WARNING: #original_exception is deprecated. ★Use #cause instead. (called from block in render_exception_with_web_console at /usr/local/rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/extensions.rb:16) Started GET "/bots?utf8=%E2%9C%93&b_search%5Btitle%5D=%E5%B9%BC%E5%A5%B3%E6%88%A6%E8%A8%98%E3%80%80%E7%AC%AC1%E8%A9%B1%E3%80%8C%E3%83%A9%E3%82%A4%E3%83%B3%E3%81%AE%E6%82%AA%E9%AD%94%E3%80%8D%09&button=" for 126.129.108.242 at 2017-02-22 01:25:52 +0000 Cannot render console from 126.129.108.242! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by BotsController#index as HTML Parameters: {"utf8"=>"✓", "b_search"=>{"title"=>"幼女戦記 第1話「ラインの悪魔」\t"}, "button"=>""} Rendering bots/index.html.erb within layouts/application Bot Load (0.7ms) SELECT "bots".* FROM "bots" Rendered bots/index.html.erb within layouts/application (14.7ms) Completed 200 OK in 206ms (Views: 201.7ms | ActiveRecord: 1.5ms)
###試したこと
bots以外のモデルなしのコントローラに同様の設定を行ってもやはりダメでした。
###補足情報(言語/FW/ツール等のバージョンなど)
rails5.0.1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/02/24 03:32
2017/02/24 03:48
2017/02/24 03:59
2017/02/24 04:17
2017/02/24 04:31
2017/02/24 04:39
2017/02/24 05:21
2017/02/24 07:41
2017/02/24 09:19