Permalink
Browse files

Update to new version of Getting Started Guide - much more Railsish a…

…nd works on beta 3
  • Loading branch information...
1 parent e6d92a6 commit b300d11a7202fdc3884ff314bbfce028c7bc3efc @mikel committed Feb 8, 2010
Showing with 578 additions and 280 deletions.
  1. +8 −0 app/controllers/application_controller.rb
  2. +18 −0 app/controllers/comments_controller.rb
  3. +5 −0 app/controllers/home_controller.rb
  4. +91 −0 app/controllers/posts_controller.rb
  5. +2 −0 app/helpers/comments_helper.rb
  6. +2 −0 app/helpers/home_helper.rb
  7. +5 −0 app/helpers/posts_helper.rb
  8. +3 −0 app/models/comment.rb
  9. +12 −0 app/models/post.rb
  10. +3 −0 app/models/tag.rb
  11. +15 −0 app/views/comments/_comment.html.erb
  12. +15 −0 app/views/comments/_form.html.erb
  13. +2 −0 app/views/home/index.html.erb
  14. +16 −0 app/views/layouts/posts.html.erb
  15. +23 −0 app/views/posts/_form.html.erb
  16. +6 −0 app/views/posts/edit.html.erb
  17. +27 −0 app/views/posts/index.html.erb
  18. +5 −0 app/views/posts/new.html.erb
  19. +30 −0 app/views/posts/show.html.erb
  20. +12 −0 app/views/tags/_form.html.erb
  21. +1 −1 config/initializers/cookie_verification_secret.rb
  22. +1 −1 config/initializers/session_store.rb
  23. +6 −0 config/routes.rb
  24. +15 −0 db/migrate/20100208023203_create_posts.rb
  25. +15 −0 db/migrate/20100208023952_create_comments.rb
  26. +14 −0 db/migrate/20100208025005_create_tags.rb
  27. +37 −0 db/schema.rb
  28. +0 −278 public/index.html
  29. +61 −0 public/stylesheets/scaffold.css
  30. +11 −0 test/fixtures/comments.yml
  31. +11 −0 test/fixtures/posts.yml
  32. +9 −0 test/fixtures/tags.yml
  33. +8 −0 test/functional/comments_controller_test.rb
  34. +8 −0 test/functional/home_controller_test.rb
  35. +45 −0 test/functional/posts_controller_test.rb
  36. +8 −0 test/unit/comment_test.rb
  37. +4 −0 test/unit/helpers/comments_helper_test.rb
  38. +4 −0 test/unit/helpers/home_helper_test.rb
  39. +4 −0 test/unit/helpers/posts_helper_test.rb
  40. +8 −0 test/unit/post_test.rb
  41. +8 −0 test/unit/tag_test.rb
@@ -1,3 +1,11 @@
class ApplicationController < ActionController::Base
protect_from_forgery
+
+ private
+
+ def authenticate
+ authenticate_or_request_with_http_basic do |user_name, password|
+ user_name == 'admin' && password == 'password'
+ end
+ end
end
@@ -0,0 +1,18 @@
+class CommentsController < ApplicationController
+
+ before_filter :authenticate, :only => [:destroy]
+
+ def create
+ @post = Post.find(params[:post_id])
+ @comment = @post.comments.create(params[:comment])
+ redirect_to post_path(@post)
+ end
+
+ def destroy
+ @post = Post.find(params[:post_id])
+ @comment = @post.comments.find(params[:id])
+ @comment.destroy
+ redirect_to post_path(@post)
+ end
+
+end
@@ -0,0 +1,5 @@
+class HomeController < ApplicationController
+ def index
+ end
+
+end
@@ -0,0 +1,91 @@
+class PostsController < ApplicationController
+
+ before_filter :authenticate, :except => [:index, :show]
+
+ # 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
@@ -0,0 +1,2 @@
+module CommentsHelper
+end
@@ -0,0 +1,2 @@
+module HomeHelper
+end
@@ -0,0 +1,5 @@
+module PostsHelper
+ def join_tags(post)
+ post.tags.map { |t| t.name }.join(", ")
+ end
+end
View
@@ -0,0 +1,3 @@
+class Comment < ActiveRecord::Base
+ belongs_to :post
+end
View
@@ -0,0 +1,12 @@
+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
View
@@ -0,0 +1,3 @@
+class Tag < ActiveRecord::Base
+ belongs_to :post
+end
@@ -0,0 +1,15 @@
+<p>
+ <b>Commenter:</b>
+ <%= comment.commenter %>
+</p>
+
+<p>
+ <b>Comment:</b>
+ <%= comment.body %>
+</p>
+
+<p>
+ <%= link_to 'Destroy Comment', [comment.post, comment],
+ :confirm => 'Are you sure?',
+ :method => :delete %>
+</p>
@@ -0,0 +1,15 @@
+<% form_for([@post, @post.comments.build]) do |f| %>
+ <%= f.error_messages %>
+
+ <div class="field">
+ <%= f.label :commenter %><br />
+ <%= f.text_field :commenter %>
+ </div>
+ <div class="field">
+ <%= f.label :body %><br />
+ <%= f.text_area :body %>
+ </div>
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
@@ -0,0 +1,2 @@
+<h1>Hello, Rails!</h1>
+<%= link_to "My Blog", posts_path %>
@@ -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 style="background: #EEEEEE;">
+
+<p class="notice"><%= notice %></p>
+
+<%= yield %>
+
+</body>
+</html>
@@ -0,0 +1,23 @@
+<% @post.tags.build %>
+<% form_for(@post) do |post_form| %>
+ <%= post_form.error_messages %>
+
+ <div class="field">
+ <%= post_form.label :name %><br />
+ <%= post_form.text_field :name %>
+ </div>
+ <div class="field">
+ <%= post_form.label :title %><br />
+ <%= post_form.text_field :title %>
+ </div>
+ <div class="field">
+ <%= post_form.label :content %><br />
+ <%= post_form.text_area :content %>
+ </div>
+ <h2>Tags</h2>
+ <%= render :partial => 'tags/form',
+ :locals => {:form => post_form} %>
+ <div class="actions">
+ <%= post_form.submit %>
+ </div>
+<% end %>
@@ -0,0 +1,6 @@
+<h1>Editing post</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @post %> |
+<%= link_to 'Back', posts_path %>
@@ -0,0 +1,27 @@
+<h1>Listing posts</h1>
+
+<table>
+ <tr>
+ <th>Name</th>
+ <th>Title</th>
+ <th>Content</th>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+
+<% @posts.each do |post| %>
+ <tr>
+ <td><%= post.name %></td>
+ <td><%= post.title %></td>
+ <td><%= post.content %></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,5 @@
+<h1>New post</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', posts_path %>
@@ -0,0 +1,30 @@
+<p>
+ <b>Name:</b>
+ <%= @post.name %>
+</p>
+
+<p>
+ <b>Title:</b>
+ <%= @post.title %>
+</p>
+
+<p>
+ <b>Content:</b>
+ <%= @post.content %>
+</p>
+
+<p>
+ <b>Tags:</b>
+ <%= join_tags(@post) %>
+</p>
+
+<h2>Comments</h2>
+<%= render :partial => "comments/comment",
+ :collection => @post.comments %>
+
+<h2>Add a comment:</h2>
+<%= render "comments/form" %>
+
+
+<%= link_to 'Edit Post', edit_post_path(@post) %> |
+<%= link_to 'Back to Posts', posts_path %> |
@@ -0,0 +1,12 @@
+<% form.fields_for :tags do |tag_form| %>
+ <div class="field">
+ <%= tag_form.label :name, 'Tag:' %>
+ <%= tag_form.text_field :name %>
+ </div>
+ <% unless tag_form.object.nil? || tag_form.object.new_record? %>
+ <div class="field">
+ <%= tag_form.label :_destroy, 'Remove:' %>
+ <%= tag_form.check_box :_destroy %>
+ </div>
+ <% end %>
+<% end %>
@@ -4,4 +4,4 @@
# If you change this key, all old signed cookies will become invalid!
# Make sure the secret is at least 30 characters and all random,
# no regular words or you'll be exposed to dictionary attacks.
-ActionController::Base.cookie_verifier_secret = 'b49646ea6d739eaf732c9977a35fae4928c719ce02bf81adec251b6e0301d6866083a9ea329af9dc04d35bf73c742a6e1febd947fbb0a4a8a0d57e17c9e5aa31'
+ActionController::Base.cookie_verifier_secret = 'f33f6335d4833a906db65431f40791d2c7e035d13144e746091e5b13fa3b45092e9fff4f923f4b7b8744ec5e4e683a4c3323a31055a7ce6e450d0e5175f13e30'
@@ -6,7 +6,7 @@
# no regular words or you'll be exposed to dictionary attacks.
ActionController::Base.session = {
:key => '_blog_session',
- :secret => 'fbb4abe4b094181f81debf66995a0b7780ff1a3e51057b2844f62edd3dd4ea90c7f5e4c2bead86d1994e5b83432622c33f45719573544204348e6bc47514d851'
+ :secret => '5a6015623c2ac0bcc7f2f63e72d826daeca73cb7865b055ffbc7e353b8e4a70e71543078efe7c5386822495ad3a444e5200bbebc7320ea47e7b28af663c60ff1'
}
# Use the database for sessions instead of the cookie-based default,
View
@@ -1,4 +1,10 @@
Blog::Application.routes.draw do |map|
+ resources :posts do
+ resources :comments
+ end
+
+ root :to => "home#index"
+
# The priority is based upon order of creation:
# first created -> highest priority.
@@ -0,0 +1,15 @@
+class CreatePosts < ActiveRecord::Migration
+ def self.up
+ create_table :posts do |t|
+ t.string :name
+ t.string :title
+ t.text :content
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :posts
+ end
+end
@@ -0,0 +1,15 @@
+class CreateComments < ActiveRecord::Migration
+ def self.up
+ create_table :comments do |t|
+ t.string :commenter
+ t.text :body
+ t.references :post
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :comments
+ end
+end
@@ -0,0 +1,14 @@
+class CreateTags < ActiveRecord::Migration
+ def self.up
+ create_table :tags do |t|
+ t.string :name
+ t.references :post
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :tags
+ end
+end
Oops, something went wrong.

0 comments on commit b300d11

Please sign in to comment.