修复index冲突
This commit is contained in:
commit
4e46453159
1
Gemfile
1
Gemfile
|
@ -39,4 +39,5 @@ end
|
||||||
|
|
||||||
group :test, :development do
|
group :test, :development do
|
||||||
gem "rspec-rails", ">= 2.8.1"
|
gem "rspec-rails", ">= 2.8.1"
|
||||||
|
gem 'pry-rails'
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,18 +1,8 @@
|
||||||
# encoding : utf-8
|
# encoding : utf-8
|
||||||
class BlogsController < ApplicationController
|
class BlogsController < ApplicationController
|
||||||
def index
|
def index
|
||||||
type = params[:type]
|
@newest = Post.desc(:created_at).to_a.first
|
||||||
page = params[:page].to_i
|
@recent = Post.desc(:created_at).to_a[1..2]
|
||||||
page = 1 if params[:page].nil?
|
|
||||||
max = 3
|
|
||||||
if type or type == ""
|
|
||||||
@posts = Post.paginate( :page=>page, :limit=> max ).where(type: map[type]).order(:created_at => :desc )
|
|
||||||
else
|
|
||||||
@posts = Post.paginate( :page=>page, :limit=> max ).all.order(:created_at => :desc )
|
|
||||||
end
|
|
||||||
@page = page
|
|
||||||
@has_old = @posts.to_a.size == max
|
|
||||||
@has_new = ! ( page == 1 )
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def rss
|
def rss
|
||||||
|
@ -22,6 +12,8 @@ class BlogsController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
|
@post = Post.find(params[:id])
|
||||||
|
@comments = Comment.all.where(post_id: @post.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -1,6 +1,14 @@
|
||||||
class Comment
|
class Comment
|
||||||
include Mongoid::Document
|
include Mongoid::Document
|
||||||
include Mongoid::Timestamps
|
include Mongoid::Timestamps
|
||||||
|
|
||||||
|
field :name, :type => String
|
||||||
field :content, :type => String
|
field :content, :type => String
|
||||||
|
field :email, :type=>String
|
||||||
|
|
||||||
|
belongs_to :post
|
||||||
|
|
||||||
|
validates :name, presence: true
|
||||||
|
validates :email, confirmation: true,:format => /@/
|
||||||
validates :content, presence: true
|
validates :content, presence: true
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
class Label
|
||||||
|
include Mongoid::Document
|
||||||
|
include Mongoid::Timestamps
|
||||||
|
|
||||||
|
field :type, :type => String
|
||||||
|
|
||||||
|
has_and_belongs_to_many :post
|
||||||
|
|
||||||
|
validates :type, presence: true
|
||||||
|
end
|
|
@ -12,6 +12,7 @@ class Post
|
||||||
field :visited_count, :type=>Integer, :default=>0
|
field :visited_count, :type=>Integer, :default=>0
|
||||||
|
|
||||||
has_many :comments
|
has_many :comments
|
||||||
|
has_and_belongs_to_many :labels
|
||||||
|
|
||||||
validates :title, :presence=>true, :uniqueness=> true
|
validates :title, :presence=>true, :uniqueness=> true
|
||||||
validates :content, :presence=>true, :length => { :minimum=> 30 }
|
validates :content, :presence=>true, :length => { :minimum=> 30 }
|
||||||
|
@ -28,4 +29,11 @@ class Post
|
||||||
self.save
|
self.save
|
||||||
self.visited_count
|
self.visited_count
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def sub_content
|
||||||
|
rd = Redcarpet::Render::HTML.new(:hard_wrap=>true)
|
||||||
|
md = Redcarpet::Markdown.new(rd, :autolink=>true)
|
||||||
|
sub_cont = md.render(self.content)
|
||||||
|
HTML_Truncator.truncate(sub_cont,100)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,37 +1,26 @@
|
||||||
.row
|
.row
|
||||||
.small-12.large-8.columns
|
.small-12.large-8.columns
|
||||||
h2.blog-title 科学是什么
|
h2.blog-title #{@newest.title}
|
||||||
p.ptag
|
p.ptag
|
||||||
| 分类:
|
| 分类:
|
||||||
span 技术
|
span #{@newest.type}
|
||||||
p.ptag
|
p.ptag
|
||||||
| 标签:
|
| 标签:
|
||||||
span 生活, 感悟
|
span #{@newest.labels.collect{ |label| label.name}.join(', ')}
|
||||||
.content
|
.content
|
||||||
p 没错, 科学只是哲学的一个部分, 所以此书给我最大的收获是, 科学只是一种实用主义. 只是这种实用主义更纯粹些:
|
== @newest.sub_content
|
||||||
p
|
|
||||||
| 1. 任何公理都值得怀疑
|
|
||||||
| 2. 能够验证与重复
|
|
||||||
|
|
||||||
p 瞧, 第 2 点的感觉就是说, 太阳一直东方升起, 这就是科学, 因为我们一直在重复这个结论. 但是, 我们知道, 1000万年后, 我们可能就再也看不到太阳了, 所以这个结论还需要修正. 这就是科学的本质.
|
= link_to "阅读全文 >>", blog_path(@newest)
|
||||||
|
|
||||||
p 再举个历史的例子:
|
|
||||||
|
|
||||||
p
|
|
||||||
| 1. 地球是平的
|
|
||||||
| 2. 地球是圆的
|
|
||||||
| 3. 地球是椭圆的
|
|
||||||
p.read-more 阅读全文 >>
|
|
||||||
p.published-at 发表于 2014-2-12
|
p.published-at 发表于 2014-2-12
|
||||||
|
|
||||||
h4.recent-title RECENT
|
h4.recent-title RECENT
|
||||||
ul.recent-content
|
ul.recent-content
|
||||||
li 你是我的朋友
|
- @recent.each do |re|
|
||||||
li 该更新装备了
|
li = link_to "#{re.title}",blog_path(re)
|
||||||
.large-3.columns.large-offset-1
|
.large-3.columns.large-offset-1
|
||||||
h4 WELCOME
|
h4 WELCOME
|
||||||
hr
|
hr
|
||||||
p 我是李亚飞, WinDy 是我的网名, 又名风一样的男子.
|
p 我是李亚飞, WinDy 是我的网名.
|
||||||
|
|
||||||
h4 ABOUTME
|
h4 ABOUTME
|
||||||
hr
|
hr
|
||||||
|
|
|
@ -3,22 +3,62 @@ require 'spec_helper'
|
||||||
describe BlogsController do
|
describe BlogsController do
|
||||||
|
|
||||||
it 'index should get by order desc' do
|
it 'index should get by order desc' do
|
||||||
a = Post.new(title: '123', content: '123'*20, type: Post::TECH)
|
a = Post.new(title: 'a123', content: '123'*20, type: Post::TECH)
|
||||||
a.save!
|
a.save!
|
||||||
sleep 1
|
|
||||||
b = Post.new(title: '1234', content: '123'*20,type: Post::TECH)
|
b = Post.new(title: 'b1234', content: '123'*20,type: Post::TECH)
|
||||||
b.save!
|
b.save!
|
||||||
|
|
||||||
|
c = Post.new(title: 'c1234', content: '123'*20,type: Post::TECH)
|
||||||
|
c.save!
|
||||||
|
|
||||||
|
d = Post.new(title: 'd1234', content: '123'*20,type: Post::TECH)
|
||||||
|
d.save!
|
||||||
|
|
||||||
|
a.update(title: 'aaa')
|
||||||
get :index
|
get :index
|
||||||
assigns[:posts][1].title.should == a.title
|
assigns[:newest].title.should == d.title
|
||||||
|
assigns[:recent][0].title.should == c.title
|
||||||
|
assigns[:recent][1].title.should == b.title
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "index with label should get by order desc" do
|
it 'test show method' do
|
||||||
a = Post.new(title: '123', content: '123'*20, type: Post::TECH)
|
post = Post.new(title: 'a123', content: '123'*20, type: Post::TECH)
|
||||||
|
post.save!
|
||||||
|
a = Comment.new(name: '1',content: '2432423',email: '22@.com')
|
||||||
|
a.post = post
|
||||||
a.save!
|
a.save!
|
||||||
sleep 1
|
b = Comment.new(name: '2',content: 'iloveyou',email: 'liuzhen@.com')
|
||||||
b = Post.new(title: '1234', content: '123'*20,type: Post::TECH)
|
b.post = post
|
||||||
b.save!
|
b.save!
|
||||||
get :index, :type=> "tech"
|
get :show, id: post.id
|
||||||
assigns[:posts][1].title.should == a.title
|
assigns[:comments][0].name.should == '1'
|
||||||
|
assigns[:comments][0].content.should == '2432423'
|
||||||
|
assigns[:comments][0].email.should == '22@.com'
|
||||||
|
assigns[:comments][1].name.should == '2'
|
||||||
|
assigns[:comments][1].content.should == 'iloveyou'
|
||||||
|
assigns[:comments][1].email.should == 'liuzhen@.com'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'test show method' do
|
||||||
|
post = Post.new(title: 'a123', content: '123'*20, type: Post::TECH)
|
||||||
|
post.save!
|
||||||
|
a = Comment.new(name: '1',content: '2432423',email: '22@.com')
|
||||||
|
a.post = post
|
||||||
|
a.save!
|
||||||
|
b = Comment.new(name: '2',content: 'iloveyou',email: 'liuzhen@.com')
|
||||||
|
b.post = post
|
||||||
|
b.save!
|
||||||
|
label = Label.new(type: '生活')
|
||||||
|
post.labels << label
|
||||||
|
post.save!
|
||||||
|
get :show, id: post.id
|
||||||
|
assigns[:comments][0].name.should == '1'
|
||||||
|
assigns[:comments][0].content.should == '2432423'
|
||||||
|
assigns[:comments][0].email.should == '22@.com'
|
||||||
|
assigns[:comments][1].name.should == '2'
|
||||||
|
assigns[:comments][1].content.should == 'iloveyou'
|
||||||
|
assigns[:comments][1].email.should == 'liuzhen@.com'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
describe Comment do
|
describe Comment do
|
||||||
it "comment should not blank" do
|
it 'test comment' do
|
||||||
a = Comment.new
|
a = Comment.new(name: '1',content: '2432423',email: '22@.com')
|
||||||
a.save.should == false
|
|
||||||
a = Comment.new(content: '11')
|
|
||||||
a.save.should == true
|
a.save.should == true
|
||||||
|
b = Comment.new(content: '2432ddd423',email: '22@.com')
|
||||||
|
b.save.should == false
|
||||||
|
b = Comment.new(name: '2', email: '22@.com')
|
||||||
|
b.save.should == false
|
||||||
|
b = Comment.new(name: '2', content: '2432ddd423',email: '22.com')
|
||||||
|
b.save.should == false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe Label do
|
||||||
|
|
||||||
|
end
|
Loading…
Reference in New Issue