支持翻页功能
This commit is contained in:
parent
90a983fc49
commit
b5bc17a2d0
1
Gemfile
1
Gemfile
|
@ -7,6 +7,7 @@ gem 'rails', '3.2.6'
|
||||||
# gem 'rails', :git => 'git://github.com/rails/rails.git'
|
# gem 'rails', :git => 'git://github.com/rails/rails.git'
|
||||||
|
|
||||||
gem "mongoid"
|
gem "mongoid"
|
||||||
|
gem "mongoid-pagination"
|
||||||
gem "bson_ext"
|
gem "bson_ext"
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -80,6 +80,9 @@ GEM
|
||||||
activemodel (~> 3.1)
|
activemodel (~> 3.1)
|
||||||
mongo (<= 1.6.2)
|
mongo (<= 1.6.2)
|
||||||
tzinfo (~> 0.3.22)
|
tzinfo (~> 0.3.22)
|
||||||
|
mongoid-pagination (0.2.0)
|
||||||
|
activesupport
|
||||||
|
mongoid
|
||||||
multi_json (1.3.6)
|
multi_json (1.3.6)
|
||||||
net-scp (1.0.4)
|
net-scp (1.0.4)
|
||||||
net-ssh (>= 1.99.1)
|
net-ssh (>= 1.99.1)
|
||||||
|
@ -167,6 +170,7 @@ DEPENDENCIES
|
||||||
jquery-rails
|
jquery-rails
|
||||||
mini_magick
|
mini_magick
|
||||||
mongoid
|
mongoid
|
||||||
|
mongoid-pagination
|
||||||
nokogiri
|
nokogiri
|
||||||
rails (= 3.2.6)
|
rails (= 3.2.6)
|
||||||
redcarpet
|
redcarpet
|
||||||
|
|
|
@ -14,3 +14,4 @@
|
||||||
//= require jquery_ujs
|
//= require jquery_ujs
|
||||||
//= require 'jquery.html5-fileupload'
|
//= require 'jquery.html5-fileupload'
|
||||||
//= require_tree .
|
//= require_tree .
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,10 @@
|
||||||
# Place all the behaviors and hooks related to the matching controller here.
|
# Place all the behaviors and hooks related to the matching controller here.
|
||||||
# All this logic will automatically be available in application.js.
|
# All this logic will automatically be available in application.js.
|
||||||
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
|
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
|
||||||
|
|
||||||
|
$(document).ready ->
|
||||||
|
$('.pagination').click ->
|
||||||
|
l = window.location.pathname
|
||||||
|
page = $(this).attr('data-page')
|
||||||
|
window.location= l + "?page=#{page}"
|
||||||
|
false
|
||||||
|
|
|
@ -8,14 +8,14 @@
|
||||||
* You're free to add application-wide styles to this file and they'll appear at the top of the
|
* You're free to add application-wide styles to this file and they'll appear at the top of the
|
||||||
* compiled file, but it's generally better to create a new file per style scope.
|
* compiled file, but it's generally better to create a new file per style scope.
|
||||||
*
|
*
|
||||||
*= require_self
|
|
||||||
*= require_tree .
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* http://meyerweb.com/eric/tools/css/reset/
|
/* http://meyerweb.com/eric/tools/css/reset/
|
||||||
v2.0 | 20110126
|
v2.0 | 20110126
|
||||||
License: none (public domain)
|
License: none (public domain)
|
||||||
*/
|
*/
|
||||||
|
//= require_self
|
||||||
|
//= require_tree .
|
||||||
|
|
||||||
html, body, div, span, applet, object, iframe,
|
html, body, div, span, applet, object, iframe,
|
||||||
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
|
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
|
||||||
|
|
|
@ -53,3 +53,13 @@ div#disqus_thread {
|
||||||
border-top: 1px solid #CCC;
|
border-top: 1px solid #CCC;
|
||||||
padding-top: 20px;
|
padding-top: 20px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
div.has_new, div.has_old {
|
||||||
|
padding-right: 1em;
|
||||||
|
a {
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
div.has_new {
|
||||||
|
float: right;
|
||||||
|
}
|
||||||
|
|
|
@ -2,11 +2,17 @@
|
||||||
class BlogsController < ApplicationController
|
class BlogsController < ApplicationController
|
||||||
def index
|
def index
|
||||||
type = params[:type]
|
type = params[:type]
|
||||||
|
page = params[:page].to_i
|
||||||
|
page = 1 if params[:page].nil?
|
||||||
|
max = 3
|
||||||
if type or type == ""
|
if type or type == ""
|
||||||
@posts = Post.where(type: map[type]).order(:created_at => :desc )
|
@posts = Post.paginate( :page=>page, :limit=> max ).where(type: map[type]).order(:created_at => :desc )
|
||||||
else
|
else
|
||||||
@posts = Post.all.order(:created_at => :desc )
|
@posts = Post.paginate( :page=>page, :limit=> max ).all.order(:created_at => :desc )
|
||||||
end
|
end
|
||||||
|
@page = page
|
||||||
|
@has_old = @posts.to_a.size == max
|
||||||
|
@has_new = ! ( page == 1 )
|
||||||
end
|
end
|
||||||
|
|
||||||
def rss
|
def rss
|
||||||
|
|
|
@ -5,6 +5,7 @@ class Post
|
||||||
CREATOR = "创业"
|
CREATOR = "创业"
|
||||||
include Mongoid::Document
|
include Mongoid::Document
|
||||||
include Mongoid::Timestamps
|
include Mongoid::Timestamps
|
||||||
|
include Mongoid::Pagination
|
||||||
field :title, :type => String
|
field :title, :type => String
|
||||||
field :content, :type => String
|
field :content, :type => String
|
||||||
field :type, :type=> String
|
field :type, :type=> String
|
||||||
|
|
|
@ -1,4 +1,16 @@
|
||||||
<div class="blogs">
|
<div class="blogs">
|
||||||
|
<% if @posts.size == 0 %>
|
||||||
|
<div class="nocontent">没有新的内容, 感谢你的光临.</div>
|
||||||
|
<% else %>
|
||||||
<%= render :partial=> "post", :collection=> @posts %>
|
<%= render :partial=> "post", :collection=> @posts %>
|
||||||
|
<% end %>
|
||||||
|
<% if @has_old %>
|
||||||
|
<div class="has_old"><a href class="pagination" data-page="<%= @page + 1 %>">← 更早博客</a></div>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
|
<% if @has_new %>
|
||||||
|
<div class="has_new"><a href class="pagination" data-page="<%= @page - 1 %>">更新博客 →</a></div>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue