読者です 読者をやめる 読者になる 読者になる

DELAEMON BLOG

Live as if you were to die tomorrow. Learn as if you were to live forever.

Getting Up and Running Quickly with Scaffolding

$ rails generate scaffold Post name:string title:string content:text
      invoke  active_record
      create    db/migrate/20120925151907_create_posts.rb
      create    app/models/post.rb
      invoke    test_unit
      create      test/unit/post_test.rb
      create      test/fixtures/posts.yml
      invoke  resource_route
       route    resources :posts
      invoke  scaffold_controller
      create    app/controllers/posts_controller.rb
      invoke    erb
      create      app/views/posts
      create      app/views/posts/index.html.erb
      create      app/views/posts/edit.html.erb
      create      app/views/posts/show.html.erb
      create      app/views/posts/new.html.erb
      create      app/views/posts/_form.html.erb
      invoke    test_unit
      create      test/functional/posts_controller_test.rb
      invoke    helper
      create      app/helpers/posts_helper.rb
      invoke      test_unit
      create        test/unit/helpers/posts_helper_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/posts.js.coffee
      invoke    scss
      create      app/assets/stylesheets/posts.css.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.css.scss

$ cat db/migrate/20120925151907_create_posts.rb 
class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.string :name
      t.string :title
      t.text :content

      t.timestamps
    end
  end
end

$ rake db:migrate
==  CreatePosts: migrating ====================================================
-- create_table(:posts)
   -> 0.0012s
==  CreatePosts: migrated (0.0012s) ===========================================
$ vim app/views/home/index.html.erb
#リンク追加
<%= link_to "My Blog", posts_path %>

$ vim app/models/post.rb
#追記 validates 
class Post < ActiveRecord::Base
  attr_accessible :content, :name, :title
 
  validates :name,  :presence => true
  validates :title, :presence => true,
                    :length => { :minimum => 5 }
end

$ rails server