diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb new file mode 100644 index 0000000..c045e25 --- /dev/null +++ b/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 diff --git a/app/helpers/posts_helper.rb b/app/helpers/posts_helper.rb new file mode 100644 index 0000000..a7b8cec --- /dev/null +++ b/app/helpers/posts_helper.rb @@ -0,0 +1,2 @@ +module PostsHelper +end diff --git a/app/models/post.rb b/app/models/post.rb new file mode 100644 index 0000000..791dcb5 --- /dev/null +++ b/app/models/post.rb @@ -0,0 +1,2 @@ +class Post < ActiveRecord::Base +end diff --git a/app/views/layouts/posts.html.erb b/app/views/layouts/posts.html.erb new file mode 100644 index 0000000..53dc367 --- /dev/null +++ b/app/views/layouts/posts.html.erb @@ -0,0 +1,16 @@ + + + + Posts: <%= controller.action_name %> + <%= stylesheet_link_tag 'scaffold' %> + <%= javascript_include_tag :defaults %> + <%= csrf_meta_tag %> + + + +

<%= notice %>

+ +<%= yield %> + + + diff --git a/app/views/posts/_form.html.erb b/app/views/posts/_form.html.erb new file mode 100644 index 0000000..e309a82 --- /dev/null +++ b/app/views/posts/_form.html.erb @@ -0,0 +1,15 @@ +<% form_for(@post) do |f| %> + <%= f.error_messages %> + +
+ <%= f.label :title %>
+ <%= f.text_field :title %> +
+
+ <%= f.label :body %>
+ <%= f.text_area :body %> +
+
+ <%= f.submit %> +
+<% end %> diff --git a/app/views/posts/edit.html.erb b/app/views/posts/edit.html.erb new file mode 100644 index 0000000..7205802 --- /dev/null +++ b/app/views/posts/edit.html.erb @@ -0,0 +1,6 @@ +

Editing post

+ +<%= render 'form' %> + +<%= link_to 'Show', @post %> | +<%= link_to 'Back', posts_path %> diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb new file mode 100644 index 0000000..67de043 --- /dev/null +++ b/app/views/posts/index.html.erb @@ -0,0 +1,25 @@ +

Listing posts

+ + + + + + + + + + +<% @posts.each do |post| %> + + + + + + + +<% end %> +
TitleBody
<%= post.title %><%= post.body %><%= link_to 'Show', post %><%= link_to 'Edit', edit_post_path(post) %><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %>
+ +
+ +<%= link_to 'New post', new_post_path %> diff --git a/app/views/posts/new.html.erb b/app/views/posts/new.html.erb new file mode 100644 index 0000000..36ad742 --- /dev/null +++ b/app/views/posts/new.html.erb @@ -0,0 +1,5 @@ +

New post

+ +<%= render 'form' %> + +<%= link_to 'Back', posts_path %> diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb new file mode 100644 index 0000000..8a91576 --- /dev/null +++ b/app/views/posts/show.html.erb @@ -0,0 +1,13 @@ +

+ Title: + <%= @post.title %> +

+ +

+ Body: + <%= @post.body %> +

+ + +<%= link_to 'Edit', edit_post_path(@post) %> | +<%= link_to 'Back', posts_path %> diff --git a/config/routes.rb b/config/routes.rb index fbf6e64..9cf2197 100644 --- a/config/routes.rb +++ b/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. diff --git a/db/migrate/20100205042616_create_posts.rb b/db/migrate/20100205042616_create_posts.rb new file mode 100644 index 0000000..66f0afa --- /dev/null +++ b/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 diff --git a/public/stylesheets/scaffold.css b/public/stylesheets/scaffold.css new file mode 100644 index 0000000..de6669a --- /dev/null +++ b/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; +} diff --git a/test/fixtures/posts.yml b/test/fixtures/posts.yml new file mode 100644 index 0000000..3f0b290 --- /dev/null +++ b/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 diff --git a/test/functional/posts_controller_test.rb b/test/functional/posts_controller_test.rb new file mode 100644 index 0000000..03a1645 --- /dev/null +++ b/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 diff --git a/test/unit/helpers/posts_helper_test.rb b/test/unit/helpers/posts_helper_test.rb new file mode 100644 index 0000000..48549c2 --- /dev/null +++ b/test/unit/helpers/posts_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class PostsHelperTest < ActionView::TestCase +end diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb new file mode 100644 index 0000000..8afe8cc --- /dev/null +++ b/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