ruby on rails
にてkaminari
を利用しpaginate
しています。
通常は問題なく表示されているのですが、単純な検索機能であるsearch
を実行すると、添付のエラーが出てしまいます。解決策わからず、ご教示頂けますと幸甚です。
ログは以下の通りです。
ActionView::Template::Error (undefined method `total_pages' for #<Product::ActiveRecord_Relation:0x007f8da3a5c098>): 78: <!--paginationは全体でのみ表示(個人ページでは表示無し)--> 79: <div class="row"> 80: <hr style="margin:0.5rem;"> 81: <%= paginate @products %> 82: <hr style="margin:0.5rem;"> 83: </div> 84: <!--pagination終わり--> app/views/user/products/index.html.erb:81:in `_app_views_user_products_index_html_erb__1308523924954366777_70123156285140' app/controllers/user/products_controller.rb:20:in `search'
ruby
1#controller 2class User::ProductsController < User::Base 3 before_action :authenticate_user!, except:[:index, :show, :search] 4 before_action :check_account 5 6 def index 7 if params[:user_id] 8 @user = User.find(params[:user_id]) 9 @adviser = @user.adviser 10 @products = @adviser.products 11 else 12 @products = Product.all 13 end 14 @products = @products.page(params[:page]) 15 end 16 17 def search 18 @products = Product.search(params[:q]) 19 render "index" 20 end 21end
search
はmodel
にて以下の通り定義しています。
ruby
1class Product < ApplicationRecord 2 class << self 3 def search(query) 4 rel = order("posted_at") 5 if query.present? 6 rel=rel.where( 7 "title LIKE? OR parts LIKE? OR situation LIKE? OR makeupkit LIKE? OR makeupkit_explain LIKE? OR memo LIKE? OR place LIKE?", 8 "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%" 9 ) 10 end 11 rel 12 end 13 end 14end
html
1#index.html.erb 2 3<!--smart phone search--> 4<div class="container"> 5 <div class="row"> 6 <div class="col-xs-12"> 7 <%= content_tag(:span, flash.notice) if flash.notice %> 8 <%= content_tag(:span, flash.alert) if flash.alert %> 9 <div> 10 </div> 11 12 <div class="row"> 13 <div class="hidden-md-up" style="height:3rem;"> 14 <%= form_tag :search_user_products, class: "col-sm-6 input-group ", method: :get do %> 15 <%= text_field_tag "q", params[:q], class: "form-control", placeholder: "目元/チーク/ヘアアレンジ..." %> 16 <span class="input-group-btn"> 17 <%= submit_tag "Go!", class: "btn btn-secondary" %> 18 </span> 19 <% end %> 20 <br> 21 </div><!--hidden-md-up--> 22 </div> 23 24 <% if @user.present? && @user == current_user then %> 25 <div class = "hidden-sm-down"> 26 <h4><%= @user.user_name + "さんの出品商品一覧" %> 27 | <%= link_to "<i class='fa fa-heart'></i>".html_safe + "いいね!一覧", :voted_user_products %> 28 | <%= link_to "<i class='fa fa-user'></i> ".html_safe + @user.user_name + "さんのアカウント", :user_account %> 29 </h4> 30 <%= link_to "レッスンを作る", new_user_user_product_path(current_user, @product), class: "btn bg-danger btn-block" %> 31 </div><!--hidden-sm down--> 32 <div class = "hidden-sm-up"> 33 <%= @user.user_name + "さんの出品商品一覧" %> 34 <%= link_to "レッスンを作る", new_user_user_product_path(current_user, @product), class: "btn bg-danger btn-block" %> 35 </div><!--hidden-sm-up--> 36 <% end %> 37 38 <!--lessones index--> 39 <div class="row"> 40 <% @products.each do |p| %> 41 <div class="col-sm-6 col-lg-4"> 42 <div class= 'card card-top-each'> 43 <div class="card-block card-user-name"> 44 <%= user_icon_tag p.adviser.user, :alt => "icon-top", :class =>"icon-top" %> 45 <%= p.adviser.user.user_name %><span style="font-size:0.5rem;">さん</span> 46 <% if (count = p.votes.count) == 0 %> 47 <span class="top-like"> 48 <i class="fa fa-heart-o" aria-hidden="true">  </i> 49 </span> 50 <% elsif (count = p.votes.count) > 0 %> 51 <span class="top-like"> 52 <i class='fa fa-heart'> <%= count %></i> 53 </span> 54 <% end %> 55 </div> 56 57 <%= link_to [:user, p ], class:"card-link" do %> 58 <div class= 'wwrapper' > 59 <%= image_tag("make_1.jpg", :class => "card-image", :alt => "profile_image") %> 60 <div class="card-img-overlay productimg"> 61 <p class="card-title productbrand"><%= p.title %><p> 62 </div> 63 </div> 64 <div class="card-block card-block-for-tag"> 65 <span class="tag tag-warning"><%= p.parts %></span> 66 <span class="tag tag-danger"><%= p.situation %></span> 67 <span class="tag tag-success">アイライナー</span> 68 <h6 class="p-price "><%= number_with_delimiter(p.price) %>円</h6> 69 <h6 class="top-place"><%= p.place %></h6> 70 </div><!--card-block--> 71 <% end %><!--end of link_to--> 72 73 </div><!--card--> 74 </div><!--col--> 75 <% end %> 76 </div><!--row--> 77 78 <!--pagination--> 79 <div class="row"> 80 <hr style="margin:0.5rem;"> 81 <%= paginate @products %> 82 <hr style="margin:0.5rem;"> 83 </div> 84 <!--pagination終わり--> 85 86</div><!--container-->
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。