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

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

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby on Rails 4

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

Q&A

解決済

1回答

1407閲覧

gem"kaminari"を使用してページネーションを作成したいのですが、表示されません。

Tomoaki_Fukuda

総合スコア75

Ruby on Rails 4

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

0グッド

1クリップ

投稿2016/10/11 17:35

###前提・実現したいこと
Ruby on railsにてイベント一覧が表示されるシステムを作っています。
ページネーション機能を実装中に以下の問題が発生しました。

###発生している問題

VIEWファイルに記載しているのにもかかわらず、
ページネーションが全く表示されない。
以下にコードを記載致します。
何卒ご教示のほど、よろしくお願いいたします。

###top.html.erb

ruby

1<% if user_signed_in? %> 2 <aside class="sidebar"> 3 4 <section> 5 <span class="label">ユーザー情報</span> 6 <h3 class="user-info"><%= @user.name %></h3> 7 </section> 8 <section> 9 <h3 class="user-info"> 10 <%= image_for(@user) %> 11 </h3> 12 </section> 13 <% if current_user?(@user) %> 14 <section> 15 <small><%= link_to 'プロフィールを編集', edit_user_path(@user) %></small> 16 <% else %> 17 </section> 18 <% end %> 19 </aside> 20 21 <ul class="notes"> 22 <h2>イベントの一覧</h2> 23 <%= render @events %> 24 <%= paginate @events, :window => 2 %> 25 </ul> 26 27 28<% else %> 29 <div class="top-wrapper"> 30 <%= image_tag "gatebook_cover.png" %> 31 <div class="register-wrapper"> 32 <h1><%= @message %></h1> 33 <%= link_to "新規登録", new_user_registration_path, class: "btn btn-large register-btn" %> 34 </div> 35 </div> 36<% end %>

###home.controller.rb

ruby

1class HomeController < ApplicationController 2 PER = 10 3 4def top 5 if user_signed_in? 6 @note = Note.new 7 @notes = Note.all.order(created_at: :desc) 8 @event = Event.new 9 @events = Event.all.order(created_at: :desc).page(params[:page]).per(10) 10 @user = User.new 11 else 12 @message = "ようこそプロトタイプサイトへ!" 13 end 14 end 15 16 def index 17 end 18 19 def about 20 end 21 22 include ApplicationHelper 23end

###event.rb

ruby

1class Event < ActiveRecord::Base 2 3validates :user_id, presence: true 4validates :name, presence: true 5validates :place, presence: true, length: { maximum: 100 } 6validates :content, presence: true, length: { maximum: 2000 } 7validate :start_time 8validate :end_time 9validate :start_time_should_be_before_end_time 10 11belongs_to :user 12 13paginates_per 2 14 15private 16 17def start_time_should_be_before_end_time 18 return unless start_time && end_time 19 20 if start_time >= end_time 21 errors.add('開始時間は終了時間よりも前に設定してください') 22 end 23end 24 25def set_eventimage(file) 26 if !file.nil? 27 file_name = file.original_filename 28 File.open("public/event_images/#{file_name}", 'wb'){|f| f.write(file.read)} 29 self.image = file_name 30 end 31end 32 33 34end

###Gemfile

source 'https://rubygems.org' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.2.5' # Use sqlite3 as the database for Active Record gem 'sqlite3' # Use SCSS for stylesheets gem 'sass-rails', '~> 5.0' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # Use CoffeeScript for .coffee assets and views gem 'coffee-rails', '~> 4.1.0' # See https://github.com/rails/execjs#readme for more supported runtimes # gem 'therubyracer', platforms: :ruby # Use jquery as the JavaScript library gem 'jquery-rails' # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks gem 'turbolinks' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.0' # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', '~> 0.4.0', group: :doc # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' # Use Unicorn as the app server # gem 'unicorn' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug' end group :development do # Access an IRB console on exception pages or by using <%= console %> in views gem 'web-console', '~> 2.0' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' source 'http://rubygems.org' gem 'hirb' gem 'devise', '3.5.1' gem 'kaminari' end

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

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

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

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

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

hana-da

2016/10/11 23:40

表示されないのは <%= render @events %> と <%= paginate @events, :window => 2 %> のどちらでしょうか?どちらともですか?
guest

回答1

0

自己解決

自己解決致しました。ご対応ありがとうございました。

投稿2016/10/16 10:09

Tomoaki_Fukuda

総合スコア75

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

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

hana-da

2016/10/16 11:05

自己解決おめでとうござます! どのように解決したのか書いていただけると非常に参加になります
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問