###実現したいこと
ruby on rails(ruby)でフォームオブジェクトを使って検索機能を実装して、
indexアクション内で、
検索していないときは、投稿一覧を表示させ、
検索したときは、検索結果を表示したいと考えています。
しかし、コントローラのストロングパラメータの設定部分で
param is missing or the value is empty: order_search_form
というエラーが発生していまいます。
ただし、検索した後・・・例えば、以下の様なURLになっているときは、正常に動きます。
http://localhost:3000/orders/index?utf8=%E2%9C%93&order_search_form%5Bitem_name_history%5D=&button=
indexにアクセスしたばかりの以下のURLでは、エラーが発生してしまいます。
http://localhost:3000/orders/index
おそらくindexページにアクセスした時点ではまだ「検索されていない」為、
パラメータが存在しないことが原因だと思うのですが、
どうやって解決したらいいのかが分からず困っています。
解決方法を教えて頂ければ幸いです。
以下が実際に書いたコードになります。
###現状のコード
/app/forms/order_search_form.rb
ruby
1#検索用のフォームオブジェクト 2class OrderSearchForm 3 4 include ActiveModel::Model 5 6 attr_accessor :item_name_history 7 8 9 #アイテム名で検索する 10 rel = BuyOrderDetail.where(['item_name_history LIKE ?', "%#{item_name_history}%"]) 11 12 end 13end
コントローラへの記述
http://localhost:3000/orders/indexにアクセスすると、search_params = params.require(:order_search_form).permit(:item_name_history)#, :created_at)の部分に、param is missing or the value is emptyというエラーが発生します。
ruby
1class OrdersController < ApplicationController 2 def index 3 #一覧表示用 4 @order = Order.all 5 #検索用 6 @order_search = OrderSearchForm.new(search_params) 7 @order_search1 = @order_search.search 8 end 9 private 10 def search_params 11 search_params = params.require(:order_search_form).permit(:item_name_history)#, :created_at) 12 end 13end
indexのビュー
<div class = "row"> <div class = "col-md-3"> <div class="panel panel-default"> <div class="panel-heading"> 詳細検索 </div> <div class="form-group"> <p>フォームオブジェクトで検索2(実験中)</p> <p>検索結果に移動する</p> <% @order_search = ItemSearchForm.new unless @order_search %> <%= form_for @order_search, url: orders_path, method: :get do |f| %> <%= f.label :item_name_history, 'アイテム検索' %> <%= f.text_field :item_name_history %></br> <%= button_tag sanitize('<i class="glyphicon glyphicon-search"></i>'), :type => "submit",:class =>"btn btn-default" %><!-- 検索アイコンボタンを表示 --> <% end %> </div> </div> </div> </div>
回答1件
あなたの回答
tips
プレビュー