给 Post 添加 Status 属性
Terminal and db/migrate/2010..._add_status_to_posts.rb
# Terminal
> rails g migration add_status_to_posts status:string
_______________________________
# 2010..._add_status_to_posts.rb
class AddStatusToPosts < ActiveRecord::Migration
def self.up
add_column :posts, :status, :string, :default => "pending"
end
...
end
_______________________________
# Terminal
> rake db:migrate
修改 Post 表单
app/views/posts/_form.html.haml
...
= simple_form_for @post do |f|
...
= f.input :status, :as => :select, :collection => ['pending', 'published'], :include_blank => false
...
添加 pending 到 Post Model
app/models/post.rb
class Post < ActiveRecord::Base
...
def pending?
status == 'pending'
end
end
一些 Helper 方法
为了让我们的 index 更‘干净’,我们在这里创建两个 辅助方法 app/helpers/post_helpers.rb
module PostsHelper
...
def sequence_display(post)
post.pending? ? image_tag('cone.jpeg', :title => 'Post Under Construction') : "##{post.sequence.to_s}"
end
def post_title(post)
post.pending? ? post.title + " (Under Construction)" : link_to(post.title, post_url(post.sequence))
end
end
更改 Post Index 页面(用到上面的 helper)
app/views/posts/index.html.haml
...
- @posts.each do |post|
.postShow
.sequence= sequence_display(post)
.title= post_title(post)
拒绝访问 Pending 的 Posts
app/controllers/post_controller.rb
class PostsController < ApplicationController
...
def show
@post = Post.find_by_sequence(params[:id])
redirect_to(posts_url, :alert => "Sorry, but that post is unavailable.") and return if @post.pending? && !admin?
...
end
end
相同道理,修改 Feed 页面
app/views/posts/index.atom.builder
atom_feed do |feed|
...
for post in @posts
unless post.pending?
feed.entry(post, :url => post_url(post.sequence)) do |entry|
...
end
end
end
end
再次同上,我们需要修改 post_link 辅助方法
app/helpers/post_helper.rb
module PostsHelper
def post_link(position)
found_post = @post.post(position)
link = link_to("#{position.to_s.capitalize} Post", post_url(found_post.sequence)) if found_post
!found_post.nil? && found_post.pending? && !admin? ? "(The #{position.to_s} post is under construction.)" : link
end
...
end
给 Logo 添加更多不同的样式
public/stylesheets/sass/application.sass
...
#logo
+text-shadow(#AAAAAA, 3px, 3px, 3px)
+box-shadow(#AAAAAA, 3px, 3px, 3px, 3px, false)
+transform(1, -15deg, 7px, 5px, 5deg)
:width 256px
:padding 5px 10px
:border 5px solid $darkbrown
:color $logo
:font
:size 40px
:family "Matiz", "Lucida Grande"
:weight bold
...
添加 ‘一定’ 的背景色
public/stylesheets/sass/application.sass
$bkg1: #F2F2F2
$bkg2: #DBC5D0
html
+min-height(100%)
body.bp
+min-height(100%)
:background-color $bkg1
+linear-gradient(color-stops($bkg1, $bkg1 40%, $bkg2))