Permalink
Browse files

Generate Post resource with scaffolding.

  • Loading branch information...
1 parent 6a88fe3 commit de8404eedcfd07209fa58fa0b56f180e62ed825b @chriseppstein committed Oct 2, 2008
@@ -0,0 +1,85 @@
+class PostsController < ApplicationController
+ # GET /posts
+ # GET /posts.xml
+ def index
+ @posts = Post.find(:all)
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.xml { render :xml => @posts }
+ end
+ end
+
+ # GET /posts/1
+ # GET /posts/1.xml
+ def show
+ @post = Post.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.xml { render :xml => @post }
+ end
+ end
+
+ # GET /posts/new
+ # GET /posts/new.xml
+ def new
+ @post = Post.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.xml { render :xml => @post }
+ end
+ end
+
+ # GET /posts/1/edit
+ def edit
+ @post = Post.find(params[:id])
+ end
+
+ # POST /posts
+ # POST /posts.xml
+ def create
+ @post = Post.new(params[:post])
+
+ respond_to do |format|
+ if @post.save
+ flash[:notice] = 'Post was successfully created.'
+ format.html { redirect_to(@post) }
+ format.xml { render :xml => @post, :status => :created, :location => @post }
+ else
+ format.html { render :action => "new" }
+ format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /posts/1
+ # PUT /posts/1.xml
+ def update
+ @post = Post.find(params[:id])
+
+ respond_to do |format|
+ if @post.update_attributes(params[:post])
+ flash[:notice] = 'Post was successfully updated.'
+ format.html { redirect_to(@post) }
+ format.xml { head :ok }
+ else
+ format.html { render :action => "edit" }
+ format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /posts/1
+ # DELETE /posts/1.xml
+ def destroy
+ @post = Post.find(params[:id])
+ @post.destroy
+
+ respond_to do |format|
+ format.html { redirect_to(posts_url) }
+ format.xml { head :ok }
+ end
+ end
+end
@@ -0,0 +1,2 @@
+module PostsHelper
+end
View
@@ -0,0 +1,2 @@
+class Post < ActiveRecord::Base
+end
@@ -0,0 +1,17 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+ <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
+ <title>Posts: <%= controller.action_name %></title>
+ <%= stylesheet_link_tag 'scaffold' %>
+</head>
+<body>
+
+<p style="color: green"><%= flash[:notice] %></p>
+
+<%= yield %>
+
+</body>
+</html>
@@ -0,0 +1,22 @@
+<h1>Editing post</h1>
+
+<%= error_messages_for :post %>
+
+<% form_for(@post) do |f| %>
+ <p>
+ <b>Title</b><br />
+ <%= f.text_field :title %>
+ </p>
+
+ <p>
+ <b>Body</b><br />
+ <%= f.text_area :body %>
+ </p>
+
+ <p>
+ <%= f.submit "Update" %>
+ </p>
+<% end %>
+
+<%= link_to 'Show', @post %> |
+<%= link_to 'Back', posts_path %>
@@ -0,0 +1,22 @@
+<h1>Listing posts</h1>
+
+<table>
+ <tr>
+ <th>Title</th>
+ <th>Body</th>
+ </tr>
+
+<% for post in @posts %>
+ <tr>
+ <td><%=h post.title %></td>
+ <td><%=h post.body %></td>
+ <td><%= link_to 'Show', post %></td>
+ <td><%= link_to 'Edit', edit_post_path(post) %></td>
+ <td><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New post', new_post_path %>
@@ -0,0 +1,21 @@
+<h1>New post</h1>
+
+<%= error_messages_for :post %>
+
+<% form_for(@post) do |f| %>
+ <p>
+ <b>Title</b><br />
+ <%= f.text_field :title %>
+ </p>
+
+ <p>
+ <b>Body</b><br />
+ <%= f.text_area :body %>
+ </p>
+
+ <p>
+ <%= f.submit "Create" %>
+ </p>
+<% end %>
+
+<%= link_to 'Back', posts_path %>
@@ -0,0 +1,13 @@
+<p>
+ <b>Title:</b>
+ <%=h @post.title %>
+</p>
+
+<p>
+ <b>Body:</b>
+ <%=h @post.body %>
+</p>
+
+
+<%= link_to 'Edit', edit_post_path(@post) %> |
+<%= link_to 'Back', posts_path %>
View
@@ -1,4 +1,6 @@
ActionController::Routing::Routes.draw do |map|
+ map.resources :posts
+
# The priority is based upon order of creation: first created -> highest priority.
# Sample of regular route:
View
Binary file not shown.
@@ -0,0 +1,14 @@
+class CreatePosts < ActiveRecord::Migration
+ def self.up
+ create_table :posts do |t|
+ t.string :title
+ t.text :body
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :posts
+ end
+end
View
@@ -0,0 +1,21 @@
+# This file is auto-generated from the current state of the database. Instead of editing this file,
+# please use the migrations feature of ActiveRecord to incrementally modify your database, and
+# then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your database schema. If you need
+# to create the application database on another system, you should be using db:schema:load, not running
+# all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended to check this file into your version control system.
+
+ActiveRecord::Schema.define(:version => 1) do
+
+ create_table "posts", :force => true do |t|
+ t.string "title"
+ t.text "body"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+end
View
@@ -0,0 +1,14 @@
+ SQL (0.000209) select sqlite_version(*)
+ SQL (0.012547) CREATE TABLE schema_info (version integer)
+ SQL (0.002359) INSERT INTO schema_info (version) VALUES(0)
+ SQL (0.000000) SQLite3::SQLException: table schema_info already exists: CREATE TABLE schema_info (version integer)
+ SQL (0.000336) SELECT version FROM schema_info
+Migrating to CreatePosts (1)
+ SQL (0.002621) CREATE TABLE posts ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255) DEFAULT NULL, "body" text DEFAULT NULL, "created_at" datetime DEFAULT NULL, "updated_at" datetime DEFAULT NULL) 
+ SQL (0.002203) UPDATE schema_info SET version = 1
+ SQL (0.000432) SELECT * FROM schema_info
+ SQL (0.000417)  SELECT name
+ FROM sqlite_master
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+
+ SQL (0.000120) PRAGMA index_list(posts)
@@ -0,0 +1,74 @@
+body { background-color: #fff; color: #333; }
+
+body, p, ol, ul, td {
+ font-family: verdana, arial, helvetica, sans-serif;
+ font-size: 13px;
+ line-height: 18px;
+}
+
+pre {
+ background-color: #eee;
+ padding: 10px;
+ font-size: 11px;
+}
+
+a { color: #000; }
+a:visited { color: #666; }
+a:hover { color: #fff; background-color:#000; }
+
+.fieldWithErrors {
+ padding: 2px;
+ background-color: red;
+ display: table;
+}
+
+#errorExplanation {
+ width: 400px;
+ border: 2px solid red;
+ padding: 7px;
+ padding-bottom: 12px;
+ margin-bottom: 20px;
+ background-color: #f0f0f0;
+}
+
+#errorExplanation h2 {
+ text-align: left;
+ font-weight: bold;
+ padding: 5px 5px 5px 15px;
+ font-size: 12px;
+ margin: -7px;
+ background-color: #c00;
+ color: #fff;
+}
+
+#errorExplanation p {
+ color: #333;
+ margin-bottom: 0;
+ padding: 5px;
+}
+
+#errorExplanation ul li {
+ font-size: 12px;
+ list-style: square;
+}
+
+div.uploadStatus {
+ margin: 5px;
+}
+
+div.progressBar {
+ margin: 5px;
+}
+
+div.progressBar div.border {
+ background-color: #fff;
+ border: 1px solid gray;
+ width: 100%;
+}
+
+div.progressBar div.background {
+ background-color: #333;
+ height: 18px;
+ width: 0%;
+}
+
View
@@ -0,0 +1,9 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+one:
+ title: MyString
+ body: MyText
+
+two:
+ title: MyString
+ body: MyText
@@ -0,0 +1,45 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class PostsControllerTest < ActionController::TestCase
+ def test_should_get_index
+ get :index
+ assert_response :success
+ assert_not_nil assigns(:posts)
+ end
+
+ def test_should_get_new
+ get :new
+ assert_response :success
+ end
+
+ def test_should_create_post
+ assert_difference('Post.count') do
+ post :create, :post => { }
+ end
+
+ assert_redirected_to post_path(assigns(:post))
+ end
+
+ def test_should_show_post
+ get :show, :id => posts(:one).id
+ assert_response :success
+ end
+
+ def test_should_get_edit
+ get :edit, :id => posts(:one).id
+ assert_response :success
+ end
+
+ def test_should_update_post
+ put :update, :id => posts(:one).id, :post => { }
+ assert_redirected_to post_path(assigns(:post))
+ end
+
+ def test_should_destroy_post
+ assert_difference('Post.count', -1) do
+ delete :destroy, :id => posts(:one).id
+ end
+
+ assert_redirected_to posts_path
+ end
+end
View
@@ -0,0 +1,8 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class PostTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ def test_truth
+ assert true
+ end
+end
@@ -1,4 +1,3 @@
-require 'rubygems'
begin
require File.join(File.dirname(__FILE__), 'lib', 'haml') # From here
rescue LoadError

0 comments on commit de8404e

Please sign in to comment.