arailsdemo 11

本文详细阐述了如何通过添加Status属性到Post表单、实现序列显示和标题助手方法,以及拒绝访问Pending状态的Post,从而优化Post管理流程。同时,对Logo样式进行了微调,提升了整体用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

给 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))

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值