From b8a5cea99585ce617f4e96127fa2ec9fd1faefb4 Mon Sep 17 00:00:00 2001 From: lovelyzhang Date: Mon, 26 Dec 2016 15:36:00 +0800 Subject: [PATCH] paginate finished --- .idea/guoren.iml | 1 + .idea/workspace.xml | 406 ++++++++++++++++++++++------- Gemfile | 4 +- Gemfile.lock | 2 + app/controllers/main_controller.rb | 15 +- app/helpers/main_helper.rb | 30 +++ app/views/layouts/_nav.html.erb | 4 +- app/views/main/main.html.erb | 46 +--- db/development.sqlite3 | Bin 53248 -> 53248 bytes public/stylesheets/mystyle.css | 9 + 10 files changed, 376 insertions(+), 141 deletions(-) diff --git a/.idea/guoren.iml b/.idea/guoren.iml index 05f7d5f..a7856d0 100644 --- a/.idea/guoren.iml +++ b/.idea/guoren.iml @@ -185,6 +185,7 @@ + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 804b69c..ac1b854 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,9 +2,15 @@ - + - + + + + + + + @@ -22,7 +28,7 @@ - + - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -54,9 +149,7 @@ @@ -140,6 +237,100 @@ - @@ -338,19 +529,19 @@ + - + + - - @@ -375,19 +566,16 @@ 21 @@ -420,7 +608,9 @@ - + + + @@ -468,7 +658,9 @@ - + + + @@ -506,22 +698,7 @@ - - - - - - - - - - - - - - - @@ -600,13 +777,6 @@ - - - - - - - @@ -634,13 +804,6 @@ - - - - - - - @@ -663,13 +826,6 @@ - - - - - - - @@ -694,40 +850,106 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Gemfile b/Gemfile index f5bcd2d..efd58e0 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,6 @@ source 'https://rubygems.org' -gem 'rake' +gem 'rake' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.0.0', '>= 5.0.0.1' # Use sqlite3 as the database for Active Record @@ -27,6 +27,8 @@ gem 'jbuilder', '~> 2.5' gem 'bcrypt', '~> 3.1.7' # for windows gem 'bcrypt-ruby', '~> 3.0.0', :require => "bcrypt" +# will_paginate +gem 'will_paginate', '~> 3.1.0' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development diff --git a/Gemfile.lock b/Gemfile.lock index 4a1d941..89b5d45 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -158,6 +158,7 @@ GEM websocket-driver (0.6.4) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.2) + will_paginate (3.1.5) PLATFORMS ruby @@ -182,6 +183,7 @@ DEPENDENCIES tzinfo-data uglifier (>= 1.3.0) web-console + will_paginate (~> 3.1.0) BUNDLED WITH 1.13.6 diff --git a/app/controllers/main_controller.rb b/app/controllers/main_controller.rb index 2e0efd9..3134bc4 100644 --- a/app/controllers/main_controller.rb +++ b/app/controllers/main_controller.rb @@ -5,9 +5,20 @@ class MainController < ApplicationController def show @user = current_user - offset = 0 - @micro_posts_array = read_newest_post(offset) + page_num = params[:page] + post_type = params[:type] + + if !page_num + page_num = 1 + end + if !post_type + post_type=[1,2,3] + end + + @posts = MicroPost.where(post_type: post_type).paginate(:page => page_num, :per_page => 10).order(post_time: :desc) + @micro_posts_array = get_post(@posts) render 'main' + end def activity diff --git a/app/helpers/main_helper.rb b/app/helpers/main_helper.rb index 9556f74..f1adda2 100644 --- a/app/helpers/main_helper.rb +++ b/app/helpers/main_helper.rb @@ -29,4 +29,34 @@ module MainHelper end return micro_posts_array end + + def get_post posts + micro_posts_array = [] + if !posts.empty? + posts.each do |micro_post| + x = Hash.new() + x["username"] = micro_post.user.name + x["userpic"] = micro_post.user.picurl + x["postid"] = micro_post.id + x["title"] = micro_post.title + x["content"] = micro_post.content + case micro_post.post_type + when 1 + x["type"] = "新鲜事" + when 2 + x["type"] = "组团信息" + when 3 + x["type"] = "失物招领" + else + x["type"] = "新鲜事" + end + x["time"] = utc_time_to_local(micro_post.post_time) + x["pics"] = micro_post.pic.split(',') if micro_post.pic + x["peo_num"] = micro_post.engage_people + micro_posts_array << x + end + end + return micro_posts_array + end + end diff --git a/app/views/layouts/_nav.html.erb b/app/views/layouts/_nav.html.erb index a26c8ff..02378b6 100644 --- a/app/views/layouts/_nav.html.erb +++ b/app/views/layouts/_nav.html.erb @@ -31,10 +31,10 @@ 果仁动态
  • - 失物信息 + 组团信息
  • - 组团信息 + 失物信息
  • diff --git a/app/views/main/main.html.erb b/app/views/main/main.html.erb index abbe35a..ac0e728 100644 --- a/app/views/main/main.html.erb +++ b/app/views/main/main.html.erb @@ -60,52 +60,10 @@ <% end %> -
    -
    -
    - 公告 -
    -
    - 通知 -
    -
    -
    -
    -
    -
    - 热门状态 -
    -
    - 状态1 -
    -
    - 状态2 -
    -
    -
    -
    -
    -
    - 热门组团活动 -
    -
    - 组团活动1 -
    -
    - 组团活动2 -
    -
    -
    -
    diff --git a/db/development.sqlite3 b/db/development.sqlite3 index 6d75b0d943d607fa3dfa51f31a8e1eb26b425ca1..fcbad81957a08104f71936adbd20a8eba035aa34 100644 GIT binary patch delta 190 zcmZozz}&Ead4e>faBewa;o}-K~bCPx>=|a?)=^2}t7#bKdKbtx0$&M~V i!}oh;8Ja%Z+WlhvR0Tt$XLCA?jGj$jy4i03AproV%tP-0 delta 161 zcmZozz}&Ead4e>f*+dy