質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
RubyGems

RubyGemsはRubyによるプログラミングのためのパッケージマネジメントツールです。ユーザはこれを使用することで、Rubyライブラリのダウンロードやアップデートや、依存関係の自動解決が可能になります。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

Q&A

解決済

1回答

20403閲覧

gem:kaminari のエラー:undefined method `total_pages'

innjera

総合スコア132

RubyGems

RubyGemsはRubyによるプログラミングのためのパッケージマネジメントツールです。ユーザはこれを使用することで、Rubyライブラリのダウンロードやアップデートや、依存関係の自動解決が可能になります。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

1グッド

1クリップ

投稿2017/01/29 06:08

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

searchmodelにて以下の通り定義しています。

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">&nbsp&nbsp</i> 49 </span> 50 <% elsif (count = p.votes.count) > 0 %> 51 <span class="top-like"> 52 <i class='fa fa-heart'>&nbsp<%= 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-->
ckr👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

search actionに@products = @products.page(params[:page])を入れていないことが原因でした。
以下の通りsearch actionに追加することで解決しました。

def search
@products = Product.search(params[:q])
@products = @products.page(params[:page])
render "index"
end

投稿2017/02/04 06:11

innjera

総合スコア132

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問