###前提・実現したいこと
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
表示されないのは <%= render @events %> と <%= paginate @events, :window => 2 %> のどちらでしょうか?どちらともですか?
回答1件
あなたの回答
tips
プレビュー