Skip to content

Commit

Permalink
Add posts scaffold
Browse files Browse the repository at this point in the history
  • Loading branch information
jm committed Feb 5, 2010
1 parent 598652f commit 8f27fe5
Show file tree
Hide file tree
Showing 16 changed files with 310 additions and 0 deletions.
83 changes: 83 additions & 0 deletions app/controllers/posts_controller.rb
@@ -0,0 +1,83 @@
class PostsController < ApplicationController
# GET /posts
# GET /posts.xml
def index
@posts = Post.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
format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
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])
format.html { redirect_to(@post, :notice => 'Post was successfully updated.') }
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
2 changes: 2 additions & 0 deletions app/helpers/posts_helper.rb
@@ -0,0 +1,2 @@
module PostsHelper
end
2 changes: 2 additions & 0 deletions app/models/post.rb
@@ -0,0 +1,2 @@
class Post < ActiveRecord::Base
end
16 changes: 16 additions & 0 deletions app/views/layouts/posts.html.erb
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>Posts: <%= controller.action_name %></title>
<%= stylesheet_link_tag 'scaffold' %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
</head>
<body>

<p class="notice"><%= notice %></p>

<%= yield %>

</body>
</html>
15 changes: 15 additions & 0 deletions app/views/posts/_form.html.erb
@@ -0,0 +1,15 @@
<% form_for(@post) do |f| %>
<%= f.error_messages %>

<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/posts/edit.html.erb
@@ -0,0 +1,6 @@
<h1>Editing post</h1>

<%= render 'form' %>
<%= link_to 'Show', @post %> |
<%= link_to 'Back', posts_path %>
25 changes: 25 additions & 0 deletions app/views/posts/index.html.erb
@@ -0,0 +1,25 @@
<h1>Listing posts</h1>

<table>
<tr>
<th>Title</th>
<th>Body</th>
<th></th>
<th></th>
<th></th>
</tr>

<% @posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= 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 %>
5 changes: 5 additions & 0 deletions app/views/posts/new.html.erb
@@ -0,0 +1,5 @@
<h1>New post</h1>

<%= render 'form' %>
<%= link_to 'Back', posts_path %>
13 changes: 13 additions & 0 deletions app/views/posts/show.html.erb
@@ -0,0 +1,13 @@
<p>
<b>Title:</b>
<%= @post.title %>
</p>

<p>
<b>Body:</b>
<%= @post.body %>
</p>


<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>
2 changes: 2 additions & 0 deletions config/routes.rb
@@ -1,4 +1,6 @@
Rails3Blog::Application.routes.draw do |map|
resources :posts

# The priority is based upon order of creation:
# first created -> highest priority.

Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20100205042616_create_posts.rb
@@ -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
61 changes: 61 additions & 0 deletions public/stylesheets/scaffold.css
@@ -0,0 +1,61 @@
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; }

div.field, div.actions {
margin-bottom: 10px;
}

.notice {
color: green;
}

.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;
}
9 changes: 9 additions & 0 deletions test/fixtures/posts.yml
@@ -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
45 changes: 45 additions & 0 deletions test/functional/posts_controller_test.rb
@@ -0,0 +1,45 @@
require 'test_helper'

class PostsControllerTest < ActionController::TestCase
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:posts)
end

test "should get new" do
get :new
assert_response :success
end

test "should create post" do
assert_difference('Post.count') do
post :create, :post => posts(:one).attributes
end

assert_redirected_to post_path(assigns(:post))
end

test "should show post" do
get :show, :id => posts(:one).to_param
assert_response :success
end

test "should get edit" do
get :edit, :id => posts(:one).to_param
assert_response :success
end

test "should update post" do
put :update, :id => posts(:one).to_param, :post => posts(:one).attributes
assert_redirected_to post_path(assigns(:post))
end

test "should destroy post" do
assert_difference('Post.count', -1) do
delete :destroy, :id => posts(:one).to_param
end

assert_redirected_to posts_path
end
end
4 changes: 4 additions & 0 deletions test/unit/helpers/posts_helper_test.rb
@@ -0,0 +1,4 @@
require 'test_helper'

class PostsHelperTest < ActionView::TestCase
end
8 changes: 8 additions & 0 deletions test/unit/post_test.rb
@@ -0,0 +1,8 @@
require 'test_helper'

class PostTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end

0 comments on commit 8f27fe5

Please sign in to comment.