博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
arailsdemo 11
阅读量:5756 次
发布时间:2019-06-18

本文共 2731 字,大约阅读时间需要 9 分钟。

  hot3.png

给 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.rbclass 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'  endend

一些 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))  endend

更改 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?    ...  endend

相同道理,修改 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  endend

再次同上,我们需要修改 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: #DBC5D0html  +min-height(100%)  body.bp  +min-height(100%)  :background-color $bkg1  +linear-gradient(color-stops($bkg1, $bkg1 40%, $bkg2))

转载于:https://my.oschina.net/kelby/blog/193119

你可能感兴趣的文章
.Net组件程序设计之远程调用(二)
查看>>
ant中文教程
查看>>
Linux常用命令(一)
查看>>
WSUS数据库远端存储条件下切换域及数据库迁移
查看>>
【VMCloud云平台】SCAP(四)租户(一)
查看>>
linux释放内存的方法
查看>>
基于 Android NDK 的学习之旅----- C调用Java
查看>>
Google 或强制 OEM 预装 20 款应用,给你一个不Root的理由
查看>>
我的友情链接
查看>>
双边过滤器(Bilateral filter)
查看>>
Android图形显示系统——下层显示4:图层合成上(合成原理与3D合成)
查看>>
Windows 10 技术预览
查看>>
Tomcat http跳转https
查看>>
一个自动布署.net网站的bat批处理实例
查看>>
tomcat 安装
查看>>
AIX:物理卷及有关概念
查看>>
我的友情链接
查看>>
Centos6.6安装选包及基础场景说明
查看>>
java基础面试题-1
查看>>
深克隆与序列化效率的比较
查看>>