arailsdemo 2

使用 simple_form

#Gemfile

gem 'simple_form'
____________________________
#Terminal

> bundle   
> rails g simple_form:install

Scaffold 生成 post

> rails g scaffold post title:string sequence:integer description:text

> rake db:migrate

给 post model 添加 default_scope 和 一些 validates

app/models/post.rb

class Post < ActiveRecord::Base
  default_scope order("sequence DESC")

  validates :title, :presence => true
  validates :sequence, :presence => true,
                       :numericality => { :greater_than => 0 }
  validates :description, :presence => true
end

给 scaffold 添加一些 CSS

public/stylesheets/scaffold.css

.simple_form label {
  float: left;
  width: 100px;
  text-align: right;
  margin: 2px 10px;
}

.simple_form div.input {
  margin-bottom: 10px;
}

.simple_form div.boolean, .simple_form input[type='submit'] {
  margin-left: 120px;
}

.simple_form div.boolean label, .simple_form label.collection_radio {
  float: none;
  margin: 0;
}

.simple_form label.collection_radio {
  margin-right: 10px;
  margin-left: 2px;
}

.simple_form .error {
  clear: left;
  margin-left: 120px;
  font-size: 12px;
  color: #D00;
  display: block;
}

.simple_form .hint {
  clear: left;
  margin-left: 120px;
  font-size: 12px;
  color: #555;
  display: block;
  font-style: italic;
}

改变 Post 主页

app/views/posts/index.html.haml

%h1= "Building This Site"

- @posts.each do |post|
  .postShow
    .sequence= "#" + post.sequence.to_s
    .title= link_to post.title, post
    .date= l post.created_at, :format => :date    
    .admin
      = link_to 'Edit', edit_post_path(post)
      = link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete
    .clear

%br.clear
= link_to 'New Post', new_post_path

格式化日期

config/locales/en.yml

en:
    time:
        formats:
            date: "%B %d, %Y"

更改 Root URL

config/routes.rb

root :to => "posts#index"

并执行:

rm publi/index.html

你可能感兴趣的:(arailsdemo 2)