Ransackを使った検索画面のサンプルがあります。
http://rails.densan-labs.net/form/search_form.html
上記では検索画面(index)と、結果画面(search)が別です。
これを1つの画面(html)にしたいです。
検索画面側は、検索フォームのみ
結果画面側は、検索フォーム+結果表示部分
で構成されています。
今回は、searchの方だけで実現しようと以下のようにしましたが、
NoMethodErrorで怒られ初期表示できません。
すみませんがご教示いただけないでしょうか?
宜しくお願い致します。
@@@コントローラ
ruby
1class ProductsController < ApplicationController 2 3 def index 4 5 @q = Product.search 6 7 end 8 9 def search 10 11 if params.nil? 12 13 @q = Product.search(search_params) 14 15 @products = @q 16 .result 17 .order(availability: :desc, code: :asc) 18 .decorate 19 20 else 21 22 @q = Product.search 23 24 @products = @q 25 26 end 27 28 29 30 end 31 32 private 33 34 def search_params 35 search_conditions = %i( 36 code_cont name_cont name_kana_cont availability_true 37 price_gteq price_lteq purchase_cost_gteq purchase_cost_lteq 38 ) 39 params.require(:q).permit(search_conditions) 40 end 41 42end
@@@@ビュー
下記の<p>カウント:<%= @products.count %></p>でエラー
ruby
1<% content_for(:title) do %> 2 <h1>商品検索</h1> 3<% end %> 4 5 6 7 8 <%= search_form_for(@q, url: search_products_path, html: { method: :get, class: 'form-horizontal', role: 'form' }) do |f| %> 9 <%= render 'search_form', f: f %> 10 <% end %> 11 12 13<%= render layout: 'shared/search_result_box' do %> 14 <table class="table table-list "> 15 <thead> 16 <tr> 17 <th>販売状況</th> 18 <th>商品コード</th> 19 <th>商品名</th> 20 <th>販売価格</th> 21 <th>仕入原価</th> 22 </tr> 23 </thead> 24 25 <p>カウント:<%= @products.count %></p> 26 <% @products.each do |product| %> 27 <tbody> 28 <tr> 29 <td><%= product.sales_condition %> </td> 30 <td><%= product.code %></td> 31 <td><%= product.name %></td> 32 <td><%= product.display_price %></td> 33 <td><%= product.display_purchase_cost %></td> 34 </tr> 35 </tbody> 36 <% end %> 37 38 39 </table> 40 41<% end %>

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/04/28 01:51 編集
2017/04/28 01:56 編集
2017/04/28 01:54
2017/04/28 02:03
2017/04/28 02:32
2017/04/28 02:58 編集
2017/04/28 03:45
2017/04/28 03:56
2017/04/28 04:34
2017/04/28 05:25
2017/04/28 05:37 編集
2017/04/28 05:46
2017/04/28 05:52