検索ルートが指定していないindexになってしまいます。
解決済
回答 1
投稿
- 評価
- クリップ 0
- VIEW 920
前提・実現したいこと
検索機能を実装したいです。
・フォームへキーワード入力。
・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
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
+1
railsは、レールから降りようとするものに厳しいです。
開示されているコードだけでは理由はわかりませんが
bots_detection_pathが
bots#indexを指定しているようです。
url: {controller: :bots,action: :detection}
とベタ書きで指定しましょう。
後、余計なお世話かもしれませんが、search後indexを表示するのではダメなのですか?
検索条件を指定しないと、何も表示されないようにindex内に記述すれば、routeもhtml.erbも
一つですみますし、turbolinksとかを考えても
index上でsearchで検索indexを表示する方が負荷が少ないと思うのですが。
さらに車輪の再発明をしなくても検索に関しては
ransackという素晴らしいgemがあるので
使ってみてはいかがでしょうか?
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.12%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
2017/02/24 12:32
このように実装した理由は複雑な検索条件のカスタマイズに対応できるようにするためです。
しかし、今のところ複雑になる予定はないのでgemと#index表示を検討します^ ^
2017/02/24 12:48
キツイことを言ってしまいました
車輪の再発明は最高の学習方法ですしね。
でも、ransackの使いやすさを体験するとphperにさえ進めたくなるような完成度なのでオススメです
2017/02/24 12:59
たとえ、きつい言い方をされたとしても僕はそっちのが成長できるタイプですので大歓迎です。
そうなんですね!
魅力的ですね。。。
試したくなってきました笑
2017/02/24 13:17
2017/02/24 13:31
ransakersやransacable_scopeといった拡張機能で
and/or検索やscope経由が数行で実装できますし
strong_paramaterのような機能でセキュリティもバッチリ
Arelが標準APIになったらrailsにデフォルトで組み込むべきgemですよ
2017/02/24 13:39
やばいですね。。。
2017/02/24 14:21
拡張機能を使いこなせば、常人の考えうる検索条件はほぼ網羅してしまいます。
scopeを書くのにArelの知識が要りますが…。
2017/02/24 16:41
これは使わない手はないですね。
ネットにはカスタマイズがしにくいと書いてありそれを鵜呑みにしてしまいました。
2017/02/24 18:19