From f6d5036dd620da48624ce32494f63aa4221b05d5 Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Fri, 20 Apr 2012 10:43:50 +0200 Subject: [PATCH] Adapt "Getting started guide" code sample --- .../app/controllers/posts_controller.rb | 76 +------------------ .../code/getting_started/app/models/post.rb | 5 +- .../app/views/posts/_form.html.erb | 45 ++++------- .../app/views/posts/new.html.erb | 2 +- guides/code/getting_started/config/routes.rb | 11 +-- ...osts.rb => 20120420083127_create_posts.rb} | 3 +- guides/code/getting_started/db/schema.rb | 17 ++--- .../getting_started/test/fixtures/posts.yml | 6 +- 8 files changed, 36 insertions(+), 129 deletions(-) rename guides/code/getting_started/db/migrate/{20110901012504_create_posts.rb => 20120420083127_create_posts.rb} (77%) diff --git a/guides/code/getting_started/app/controllers/posts_controller.rb b/guides/code/getting_started/app/controllers/posts_controller.rb index 1581d4eb16f42..3373443b16ad9 100644 --- a/guides/code/getting_started/app/controllers/posts_controller.rb +++ b/guides/code/getting_started/app/controllers/posts_controller.rb @@ -1,84 +1,12 @@ class PostsController < ApplicationController - http_basic_authenticate_with :name => "dhh", :password => "secret", :except => :index - # GET /posts - # GET /posts.json - def index - @posts = Post.all - respond_to do |format| - format.html # index.html.erb - format.json { render json: @posts } - end - end - - # GET /posts/1 - # GET /posts/1.json - def show - @post = Post.find(params[:id]) - - respond_to do |format| - format.html # show.html.erb - format.json { render json: @post } - end - end - - # GET /posts/new - # GET /posts/new.json def new - @post = Post.new - - respond_to do |format| - format.html # new.html.erb - format.json { render json: @post } - end - end - - # GET /posts/1/edit - def edit - @post = Post.find(params[:id]) end - # POST /posts - # POST /posts.json 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.json { render json: @post, status: :created, location: @post } - else - format.html { render action: "new" } - format.json { render json: @post.errors, status: :unprocessable_entity } - end - end - end - - # PUT /posts/1 - # PUT /posts/1.json - 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.json { head :no_content } - else - format.html { render action: "edit" } - format.json { render json: @post.errors, status: :unprocessable_entity } - end - end - end - - # DELETE /posts/1 - # DELETE /posts/1.json - def destroy - @post = Post.find(params[:id]) - @post.destroy - - respond_to do |format| - format.html { redirect_to posts_url } - format.json { head :no_content } - end + @post.save + redirect_to :action => :index end end diff --git a/guides/code/getting_started/app/models/post.rb b/guides/code/getting_started/app/models/post.rb index 61c2b5ae44afc..4b809110b6c52 100644 --- a/guides/code/getting_started/app/models/post.rb +++ b/guides/code/getting_started/app/models/post.rb @@ -1,11 +1,10 @@ class Post < ActiveRecord::Base - validates :name, :presence => true validates :title, :presence => true, :length => { :minimum => 5 } - + has_many :comments, :dependent => :destroy has_many :tags - + accepts_nested_attributes_for :tags, :allow_destroy => :true, :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } } end diff --git a/guides/code/getting_started/app/views/posts/_form.html.erb b/guides/code/getting_started/app/views/posts/_form.html.erb index e27da7f4130eb..51fa3f4f12260 100644 --- a/guides/code/getting_started/app/views/posts/_form.html.erb +++ b/guides/code/getting_started/app/views/posts/_form.html.erb @@ -1,32 +1,15 @@ -<% @post.tags.build %> -<%= form_for(@post) do |post_form| %> - <% if @post.errors.any? %> -
-

<%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:

- -
- <% end %> - -
- <%= post_form.label :name %>
- <%= post_form.text_field :name %> -
-
- <%= post_form.label :title %>
- <%= post_form.text_field :title %> -
-
- <%= post_form.label :content %>
- <%= post_form.text_area :content %> -
-

Tags

- <%= render :partial => 'tags/form', - :locals => {:form => post_form} %> -
- <%= post_form.submit %> -
+<%= form_for :post, :url => { :action => :create } do |f| %> +

+ <%= f.label :title %>
+ <%= f.text_field :title %> +

+ +

+ <%= f.label :text %>
+ <%= f.text_area :text %> +

+ +

+ <%= f.submit %> +

<% end %> diff --git a/guides/code/getting_started/app/views/posts/new.html.erb b/guides/code/getting_started/app/views/posts/new.html.erb index 36ad7421f98ca..5d6482f880396 100644 --- a/guides/code/getting_started/app/views/posts/new.html.erb +++ b/guides/code/getting_started/app/views/posts/new.html.erb @@ -2,4 +2,4 @@ <%= render 'form' %> -<%= link_to 'Back', posts_path %> +<%#= link_to 'Back', posts_path %> diff --git a/guides/code/getting_started/config/routes.rb b/guides/code/getting_started/config/routes.rb index b048ac68f12d9..98b1a6dcc80cd 100644 --- a/guides/code/getting_started/config/routes.rb +++ b/guides/code/getting_started/config/routes.rb @@ -1,9 +1,10 @@ Blog::Application.routes.draw do - resources :posts do - resources :comments - end + # resources :posts do + # resources :comments + # end - get "home/index" + get "posts/new" + post "posts/create" # The priority is based upon order of creation: # first created -> highest priority. @@ -55,7 +56,7 @@ # You can have the root of your site routed with "root" # just remember to delete public/index.html. root :to => "home#index" - + # See how all your routes lay out with "rake routes" # This is a legacy wild controller route that's not recommended for RESTful applications. diff --git a/guides/code/getting_started/db/migrate/20110901012504_create_posts.rb b/guides/code/getting_started/db/migrate/20120420083127_create_posts.rb similarity index 77% rename from guides/code/getting_started/db/migrate/20110901012504_create_posts.rb rename to guides/code/getting_started/db/migrate/20120420083127_create_posts.rb index d45a96152356a..602bef31ab172 100644 --- a/guides/code/getting_started/db/migrate/20110901012504_create_posts.rb +++ b/guides/code/getting_started/db/migrate/20120420083127_create_posts.rb @@ -1,9 +1,8 @@ class CreatePosts < ActiveRecord::Migration def change create_table :posts do |t| - t.string :name t.string :title - t.text :content + t.text :text t.timestamps end diff --git a/guides/code/getting_started/db/schema.rb b/guides/code/getting_started/db/schema.rb index 9db4fbe4b60e7..cfb56ca9b9c7f 100644 --- a/guides/code/getting_started/db/schema.rb +++ b/guides/code/getting_started/db/schema.rb @@ -11,31 +11,30 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110901013701) do +ActiveRecord::Schema.define(:version => 20120420083127) do create_table "comments", :force => true do |t| t.string "commenter" t.text "body" t.integer "post_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end add_index "comments", ["post_id"], :name => "index_comments_on_post_id" create_table "posts", :force => true do |t| - t.string "name" t.string "title" - t.text "content" - t.datetime "created_at" - t.datetime "updated_at" + t.text "text" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end create_table "tags", :force => true do |t| t.string "name" t.integer "post_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end add_index "tags", ["post_id"], :name => "index_tags_on_post_id" diff --git a/guides/code/getting_started/test/fixtures/posts.yml b/guides/code/getting_started/test/fixtures/posts.yml index 8b0f75a33d692..e1edfd385efdb 100644 --- a/guides/code/getting_started/test/fixtures/posts.yml +++ b/guides/code/getting_started/test/fixtures/posts.yml @@ -1,11 +1,9 @@ # Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html one: - name: MyString title: MyString - content: MyText + text: MyText two: - name: MyString title: MyString - content: MyText + text: MyText