Re-do Getting Started application with Rails 4.

The sample application with the Getting Started Guide was very out of
date. I've re-done it on edge (as of 51b9def5bf) so it should
be good to go with Rails 4. It's also in synch with what the guide
actually says.
This commit is contained in:
Steve Klabnik 2013-01-22 00:12:29 -05:00
parent 51b9def5bf
commit 2214237c39
76 changed files with 406 additions and 303 deletions

16
guides/code/getting_started/.gitignore vendored Normal file
View File

@ -0,0 +1,16 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'
# Ignore bundler config.
/.bundle
# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-journal
# Ignore all logfiles and tempfiles.
/log/*.log
/tmp

View File

@ -1,38 +1,38 @@
source 'https://rubygems.org'
gem 'rails', '3.2.3'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'rails', '4.0.0'
gem 'sqlite3'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'sprockets-rails'
gem 'sass-rails'
gem 'coffee-rails'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platform => :ruby
# gem 'therubyracer', platforms: :ruby
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.0.1'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# To use Jbuilder templates for JSON
# gem 'jbuilder'
# Use unicorn as the app server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# gem 'capistrano', group: :development
# To use debugger
# gem 'debugger'

View File

@ -0,0 +1,150 @@
GIT
remote: git://github.com/rails/activerecord-deprecated_finders.git
revision: 2e7b35d7948cefb2bba96438873d7f7bb1961a03
specs:
activerecord-deprecated_finders (0.0.2)
GIT
remote: git://github.com/rails/arel.git
revision: 38d0a222e275d917a2c1d093b24457bafb600a00
specs:
arel (3.0.2.20120819075748)
GIT
remote: git://github.com/rails/coffee-rails.git
revision: 052634e6d02d4800d7b021201cc8d5829775b3cd
specs:
coffee-rails (4.0.0.beta)
coffee-script (>= 2.2.0)
railties (>= 4.0.0.beta, < 5.0)
GIT
remote: git://github.com/rails/sass-rails.git
revision: ae8138a89cac397c0df903dd533e2862902ce8f5
specs:
sass-rails (4.0.0.beta)
railties (>= 4.0.0.beta, < 5.0)
sass (>= 3.1.10)
sprockets-rails (~> 2.0.0.rc0)
tilt (~> 1.3)
GIT
remote: git://github.com/rails/sprockets-rails.git
revision: 09917104fdb42245fe369612a7b0e3d77e1ba763
specs:
sprockets-rails (2.0.0.rc1)
actionpack (>= 3.0)
activesupport (>= 3.0)
sprockets (~> 2.8)
PATH
remote: /Users/steve/src/rails
specs:
actionmailer (4.0.0.beta)
actionpack (= 4.0.0.beta)
mail (~> 2.5.3)
actionpack (4.0.0.beta)
activesupport (= 4.0.0.beta)
builder (~> 3.1.0)
erubis (~> 2.7.0)
rack (~> 1.4.3)
rack-test (~> 0.6.1)
activemodel (4.0.0.beta)
activesupport (= 4.0.0.beta)
builder (~> 3.1.0)
activerecord (4.0.0.beta)
activemodel (= 4.0.0.beta)
activerecord-deprecated_finders (= 0.0.2)
activesupport (= 4.0.0.beta)
arel (~> 3.0.2)
activesupport (4.0.0.beta)
i18n (~> 0.6)
minitest (~> 4.1)
multi_json (~> 1.3)
thread_safe (~> 0.1)
tzinfo (~> 0.3.33)
rails (4.0.0.beta)
actionmailer (= 4.0.0.beta)
actionpack (= 4.0.0.beta)
activerecord (= 4.0.0.beta)
activesupport (= 4.0.0.beta)
bundler (>= 1.2.2, < 2.0)
railties (= 4.0.0.beta)
sprockets-rails (~> 2.0.0.rc1)
railties (4.0.0.beta)
actionpack (= 4.0.0.beta)
activesupport (= 4.0.0.beta)
rake (>= 0.8.7)
rdoc (~> 3.4)
thor (>= 0.15.4, < 2.0)
GEM
remote: https://rubygems.org/
specs:
atomic (1.0.1)
builder (3.1.4)
coffee-script (2.2.0)
coffee-script-source
execjs
coffee-script-source (1.4.0)
erubis (2.7.0)
execjs (1.4.0)
multi_json (~> 1.0)
hike (1.2.1)
i18n (0.6.1)
jbuilder (1.0.2)
activesupport (>= 3.0.0)
jquery-rails (2.2.0)
railties (>= 3.0, < 5.0)
thor (>= 0.14, < 2.0)
json (1.7.6)
mail (2.5.3)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.19)
minitest (4.4.0)
multi_json (1.5.0)
polyglot (0.3.3)
rack (1.4.4)
rack-test (0.6.2)
rack (>= 1.0)
rake (10.0.3)
rdoc (3.12)
json (~> 1.4)
sass (3.2.5)
sprockets (2.8.2)
hike (~> 1.2)
multi_json (~> 1.0)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.7)
thor (0.16.0)
thread_safe (0.1.0)
atomic
tilt (1.3.3)
treetop (1.4.12)
polyglot
polyglot (>= 0.3.1)
turbolinks (1.0.0)
coffee-rails
tzinfo (0.3.35)
uglifier (1.3.0)
execjs (>= 0.3.0)
multi_json (~> 1.0, >= 1.0.2)
PLATFORMS
ruby
DEPENDENCIES
activerecord-deprecated_finders!
arel!
coffee-rails!
jbuilder (~> 1.0.1)
jquery-rails
rails!
sass-rails!
sprockets-rails!
sqlite3
turbolinks
uglifier (>= 1.0.3)

View File

@ -23,6 +23,6 @@ Things you may want to cover:
* ...
If you plan to generate application documentation with `rake doc:app` this file
is expected to be `README.rdoc`, otherwise please feel free to rename it and use
a different markup language.
Please feel free to use a different markup language if you do not plan to run
<tt>rake doc:app</tt>.

View File

@ -1,4 +1,3 @@
#!/usr/bin/env rake
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

View File

@ -12,4 +12,5 @@
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .

View File

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

View File

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

View File

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

View File

@ -10,4 +10,4 @@
*
*= require_self
*= require_tree .
*/
*/

View File

@ -0,0 +1,3 @@
// Place all the styles related to the Comments controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

View File

@ -0,0 +1,3 @@
// Place all the styles related to the posts controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

View File

@ -0,0 +1,3 @@
// Place all the styles related to the welcome controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

View File

@ -1,3 +1,5 @@
class ApplicationController < ActionController::Base
protect_from_forgery
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
end

View File

@ -1,9 +1,10 @@
class CommentsController < ApplicationController
http_basic_authenticate_with :name => "dhh", :password => "secret", :only => :destroy
  http_basic_authenticate_with name: "dhh", password: "secret", only: :destroy
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment])
@comment = @post.comments.create(params[:comment].permit(:commenter, :body))
redirect_to post_path(@post)
end
@ -13,5 +14,4 @@ class CommentsController < ApplicationController
@comment.destroy
redirect_to post_path(@post)
end
end

View File

@ -1,6 +1,6 @@
class PostsController < ApplicationController
http_basic_authenticate_with :name => "dhh", :password => "secret", :except => [:index, :show]
  http_basic_authenticate_with name: "dhh", password: "secret", except: [:index, :show]
def index
@posts = Post.all
@ -10,20 +10,6 @@ class PostsController < ApplicationController
@post = Post.find(params[:id])
end
def new
@post = Post.new
end
def create
@post = Post.new(params[:post])
if @post.save
redirect_to :action => :show, :id => @post.id
else
render 'new'
end
end
def edit
@post = Post.find(params[:id])
end
@ -31,17 +17,31 @@ class PostsController < ApplicationController
def update
@post = Post.find(params[:id])
if @post.update(params[:post])
redirect_to :action => :show, :id => @post.id
if @post.update(params[:post].permit(:title, :text))
redirect_to action: :show, id: @post.id
else
render 'edit'
end
end
def new
@post = Post.new
end
def create
@post = Post.new(params[:post].permit(:title, :text))
if @post.save
redirect_to action: :show, id: @post.id
else
render 'new'
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to :action => :index
redirect_to action: :index
end
end

View File

@ -1,6 +1,7 @@
class Post < ActiveRecord::Base
validates :title, :presence => true,
:length => { :minimum => 5 }
has_many :comments, dependent: :destroy
has_many :comments, :dependent => :destroy
validates :title,
presence: true,
length: { minimum: 5 }
end

View File

@ -10,6 +10,6 @@
<p>
<%= link_to 'Destroy Comment', [comment.post, comment],
:method => :delete,
:data => { :confirm => 'Are you sure?' } %>
method: :delete,
data: { confirm: 'Are you sure?' } %>
</p>

View File

@ -2,8 +2,8 @@
<html>
<head>
<title>Blog</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body>

View File

@ -1,7 +1,8 @@
<%= form_for @post do |f| %>
<% if @post.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
<h2><%= pluralize(@post.errors.count, "error") %> prohibited
this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
@ -10,12 +11,12 @@
</div>
<% end %>
<p>
<%= f.label :title %><br />
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br />
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
@ -23,3 +24,4 @@
<%= f.submit %>
</p>
<% end %>

View File

@ -1,5 +1,5 @@
<h1>Editing post</h1>
<h1>Edit post</h1>
<%= render 'form' %>
<%= link_to 'Back', :action => :index %>
<%= link_to 'Back', action: :index %>

View File

@ -1,7 +1,4 @@
<h1>Listing posts</h1>
<%= link_to 'New post', :action => :new %>
<h1>Listing Posts</h1>
<table>
<tr>
<th>Title</th>
@ -15,9 +12,10 @@
<tr>
<td><%= post.title %></td>
<td><%= post.text %></td>
<td><%= link_to 'Show', :action => :show, :id => post.id %>
<td><%= link_to 'Edit', :action => :edit, :id => post.id %>
<td><%= link_to 'Destroy', { :action => :destroy, :id => post.id }, :method => :delete, :data => { :confirm => 'Are you sure?' } %>
<td><%= link_to 'Show', action: :show, id: post.id %></td>
<td><%= link_to 'Edit', action: :edit, id: post.id %></td>
<td><%= link_to 'Destroy', { action: :destroy, id: post.id },
method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>

View File

@ -2,4 +2,4 @@
<%= render 'form' %>
<%= link_to 'Back', :action => :index %>
<%= link_to 'Back', action: :index %>

View File

@ -1,2 +1,3 @@
<h1>Hello, Rails!</h1>
<%= link_to "My Blog", :controller => "posts" %>
<%= link_to "My Blog", controller: "posts" %>

View File

@ -0,0 +1,4 @@
#!/usr/bin/env ruby
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'rubygems'
load Gem.bin_path('bundler', 'bundle')

View File

@ -0,0 +1,4 @@
#!/usr/bin/env ruby
APP_PATH = File.expand_path('../../config/application', __FILE__)
require_relative '../config/boot'
require 'rails/commands'

View File

@ -0,0 +1,4 @@
#!/usr/bin/env ruby
require_relative '../config/boot'
require 'rake'
Rake.application.run

View File

@ -2,12 +2,8 @@ require File.expand_path('../boot', __FILE__)
require 'rails/all'
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
# Assets should be precompiled for production (so we don't need the gems loaded then)
Bundler.require(*Rails.groups(assets: %w(development test)))
module Blog
class Application < Rails::Application
@ -17,36 +13,5 @@ module Blog
# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]
# Use SQL instead of Active Record's schema dumper when creating the database.
# This is necessary if your schema can't be completely dumped by the schema dumper,
# like if you have constraints or database-specific column types.
# config.active_record.schema_format = :sql
# Enforce whitelist mode for mass assignment.
# This will create an empty whitelist of attributes available for mass-assignment for all models
# in your app. As such, your models will need to explicitly whitelist or blacklist accessible
# parameters by using an attr_accessible or attr_protected declaration.
# config.active_record.whitelist_attributes = true
# Enable the asset pipeline.
config.assets.enabled = true
# Version of your assets, change this if you want to expire all your assets.
config.assets.version = '1.0'
end
end

View File

@ -1,5 +1,5 @@
# Load the rails application
# Load the rails application.
require File.expand_path('../application', __FILE__)
# Initialize the rails application
# Initialize the rails application.
Blog::Application.initialize!

View File

@ -6,6 +6,9 @@ Blog::Application.configure do
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
@ -19,16 +22,13 @@ Blog::Application.configure do
# Only use best-standards-support built into browsers.
config.action_dispatch.best_standards_support = :builtin
# Raise exception on mass assignment protection for ActiveRecord models.
config.active_record.mass_assignment_sanitizer = :strict
# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL).
config.active_record.auto_explain_threshold_in_seconds = 0.5
# Do not compress assets.
config.assets.compress = false
# Raise an error on page load if there are pending migrations
config.active_record.migration_error = :page_load
# Expands the lines which load the assets.
# Debug mode disables concatenation and preprocessing of assets.
config.assets.debug = true
end

View File

@ -4,24 +4,36 @@ Blog::Application.configure do
# Code is not reloaded between requests.
config.cache_classes = true
# Eager load code on boot. This eager loads most of Rails and
# your application in memory, allowing both thread web servers
# and those relying on copy on write to perform better.
# Rake tasks automatically ignore this option for performance.
config.eager_load = true
# Full error reports are disabled and caching is turned on.
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Enable Rack::Cache to put a simple HTTP cache in front of your application
# Add `rack-cache` to your Gemfile before enabling this.
# For large-scale production use, consider using a caching reverse proxy like nginx, varnish or squid.
# config.action_dispatch.rack_cache = true
# Disable Rails's static asset server (Apache or nginx will already do this).
config.serve_static_assets = false
# Compress JavaScripts and CSS.
config.assets.compress = true
config.assets.js_compressor = :uglifier
# config.assets.css_compressor = :sass
# Don't fallback to assets pipeline if a precompiled asset is missed.
# Whether to fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = false
# Generate digests for assets URLs.
config.assets.digest = true
# Defaults to nil
# config.assets.manifest = YOUR_PATH
# Version of your assets, change this if you want to expire all your assets.
config.assets.version = '1.0'
# Specifies the header that your server uses for sending files.
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
@ -30,8 +42,8 @@ Blog::Application.configure do
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
# See everything in the log (default is :info).
# config.log_level = :debug
# Set to :debug to see everything in the log.
config.log_level = :info
# Prepend all log lines with the following tags.
# config.log_tags = [ :subdomain, :uuid ]
@ -45,15 +57,14 @@ Blog::Application.configure do
# Enable serving of images, stylesheets, and JavaScripts from an asset server.
# config.action_controller.asset_host = "http://assets.example.com"
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added).
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
# config.assets.precompile += %w( search.js )
# Disable delivery errors, bad email addresses will be ignored.
# Ignore bad email addresses and do not raise email delivery errors.
# Set this to true and configure the email server for immediate delivery to raise delivery errors.
# config.action_mailer.raise_delivery_errors = false
# Enable threaded mode.
# config.threadsafe!
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found).
config.i18n.fallbacks = true
@ -64,4 +75,10 @@ Blog::Application.configure do
# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL).
# config.active_record.auto_explain_threshold_in_seconds = 0.5
# Disable automatic flushing of the log to improve performance.
# config.autoflush_log = false
# Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = ::Logger::Formatter.new
end

View File

@ -7,6 +7,11 @@ Blog::Application.configure do
# and recreated between test runs. Don't rely on the data there!
config.cache_classes = true
# Do not eager load code on boot. This avoids loading your whole application
# just for the purpose of running a single test. If you are using a tool that
# preloads Rails for running tests, you may have to set it to true.
config.eager_load = false
# Configure static asset server for tests with Cache-Control for performance.
config.serve_static_assets = true
config.static_cache_control = "public, max-age=3600"
@ -26,9 +31,6 @@ Blog::Application.configure do
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test
# Raise exception on mass assignment protection for Active Record models.
config.active_record.mass_assignment_sanitizer = :strict
# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr
end

View File

@ -0,0 +1,4 @@
# Be sure to restart your server when you modify this file.
# Configure sensitive parameters which will be filtered from the log file.
Rails.application.config.filter_parameters += [:password]

View File

@ -1,15 +1,16 @@
# Be sure to restart your server when you modify this file.
# Add new inflection rules using the following format
# (all these examples are active by default):
# ActiveSupport::Inflector.inflections do |inflect|
# Add new inflection rules using the following format. Inflections
# are locale specific, and you may define rules for as many different
# locales as you wish. All of these examples are active by default:
# ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.plural /^(ox)$/i, '\1en'
# inflect.singular /^(ox)en/i, '\1'
# inflect.irregular 'person', 'people'
# inflect.uncountable %w( fish sheep )
# end
#
# These inflection rules are supported but not enabled by default:
# ActiveSupport::Inflector.inflections do |inflect|
# ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.acronym 'RESTful'
# end

View File

@ -0,0 +1,9 @@
# Be sure to restart your server when you modify this file.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# Rails.application.config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# Rails.application.config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# Rails.application.config.i18n.default_locale = :de

View File

@ -2,8 +2,11 @@
# Your secret key for verifying the integrity of signed cookies.
# 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.
# Make sure your secret key is kept private
# You can use `rake secret` to generate a secure secret key.
# Make sure your secret_key_base is kept private
# if you're sharing your code publicly.
Blog::Application.config.secret_key_base = '685a9bf865b728c6549a191c90851c1b5ec41ecb60b9e94ad79dd3f824749798aa7b5e94431901960bee57809db0947b481570f7f13376b7ca190fa28099c459'
Blog::Application.config.secret_key_base = 'e8aab50cec8a06a75694111a4cbaf6e22fc288ccbc6b268683aae7273043c69b15ca07d10c92a788dd6077a54762cbfcc55f19c3459f7531221b3169f8171a53'

View File

@ -1,3 +1,3 @@
# Be sure to restart your server when you modify this file.
Blog::Application.config.session_store :cookie_store, key: '_blog_session'
Blog::Application.config.session_store :encrypted_cookie_store, key: '_blog_session'

View File

@ -1,14 +1,14 @@
# Be sure to restart your server when you modify this file.
#
# This file contains settings for ActionController::ParamsWrapper which
# is enabled by default.
# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
ActiveSupport.on_load(:action_controller) do
wrap_parameters format: [:json]
wrap_parameters format: [:json] if respond_to?(:wrap_parameters)
end
# Disable root element in JSON by default.
ActiveSupport.on_load(:active_record) do
self.include_root_in_json = false
end
# To enable root element in JSON for ActiveRecord objects.
# ActiveSupport.on_load(:active_record) do
# self.include_root_in_json = true
# end

View File

@ -1,5 +1,23 @@
# Sample localization file for English. Add more files in this directory for other locales.
# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
# Files in the config/locales directory are used for internationalization
# and are automatically loaded by Rails. If you want to use locales other
# than English, add the necessary files in this directory.
#
# To use the locales, use `I18n.t`:
#
# I18n.t 'hello'
#
# In views, this is aliased to just `t`:
#
# <%= t('hello') %>
#
# To use a different locale, set it with `I18n.locale`:
#
# I18n.locale = :es
#
# This would use the information in config/locales/es.yml.
#
# To learn more, please read the Rails Internationalization guide
# available at http://guides.rubyonrails.org/i18n.html.
en:
hello: "Hello world"

View File

@ -1,63 +1,7 @@
Blog::Application.routes.draw do
resources :posts do
resources :comments
end
# The priority is based upon order of creation:
# first created -> highest priority.
# Sample of regular route:
# match 'products/:id' => 'catalog#view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
# match 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# This route can be invoked with purchase_url(id: product.id)
# Sample resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Sample resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Sample resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Sample resource route with more complex sub-resources
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end
# Sample resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
root :to => "welcome#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.
# Note: This route will make all actions in every controller accessible via GET requests.
# match ':controller(/:action(/:id))(.:format)'
root to: "welcome#index"
end

View File

@ -3,10 +3,9 @@ class CreateComments < ActiveRecord::Migration
create_table :comments do |t|
t.string :commenter
t.text :body
t.references :post
t.references :post, index: true
t.timestamps
end
add_index :comments, :post_id
end
end

View File

@ -9,34 +9,25 @@
# 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.
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(:version => 20120420083127) do
ActiveRecord::Schema.define(version: 20130122045842) do
create_table "comments", :force => true do |t|
create_table "comments", force: true do |t|
t.string "commenter"
t.text "body"
t.integer "post_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "comments", ["post_id"], :name => "index_comments_on_post_id"
add_index "comments", ["post_id"], name: "index_comments_on_post_id"
create_table "posts", :force => true do |t|
create_table "posts", force: true do |t|
t.string "title"
t.text "text"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "tags", :force => true do |t|
t.string "name"
t.integer "post_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "tags", ["post_id"], :name => "index_tags_on_post_id"
end

View File

@ -2,7 +2,7 @@
<html>
<head>
<title>The page you were looking for doesn't exist (404)</title>
<style type="text/css">
<style>
body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
div.dialog {
width: 25em;
@ -22,5 +22,6 @@
<h1>The page you were looking for doesn't exist.</h1>
<p>You may have mistyped the address or the page may have moved.</p>
</div>
<p>If you are the application owner check the logs for more information.</p>
</body>
</html>

View File

@ -2,7 +2,7 @@
<html>
<head>
<title>The change you wanted was rejected (422)</title>
<style type="text/css">
<style>
body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
div.dialog {
width: 25em;

View File

@ -2,7 +2,7 @@
<html>
<head>
<title>We're sorry, but something went wrong (500)</title>
<style type="text/css">
<style>
body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
div.dialog {
width: 25em;
@ -21,5 +21,6 @@
<div class="dialog">
<h1>We're sorry, but something went wrong.</h1>
</div>
<p>If you are the application owner check the logs for more information.</p>
</body>
</html>

View File

@ -1,6 +0,0 @@
#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
APP_PATH = File.expand_path('../../config/application', __FILE__)
require File.expand_path('../../config/boot', __FILE__)
require 'rails/commands'

View File

@ -1,6 +1,6 @@
require 'test_helper'
class TagTest < ActiveSupport::TestCase
class PostsControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end

View File

@ -5,4 +5,5 @@ class WelcomeControllerTest < ActionController::TestCase
get :index
assert_response :success
end
end

View File

@ -1,11 +1,11 @@
# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
one:
commenter: MyString
body: MyText
post:
post_id:
two:
commenter: MyString
body: MyText
post:
post_id:

View File

@ -1,4 +1,4 @@
# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
one:
title: MyString

View File

@ -1,49 +0,0 @@
require 'test_helper'
class PostsControllerTest < ActionController::TestCase
setup do
@post = posts(:one)
end
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: @post.attributes
end
assert_redirected_to post_path(assigns(:post))
end
test "should show post" do
get :show, id: @post.to_param
assert_response :success
end
test "should get edit" do
get :edit, id: @post.to_param
assert_response :success
end
test "should update post" do
put :update, id: @post.to_param, post: @post.attributes
assert_redirected_to post_path(assigns(:post))
end
test "should destroy post" do
assert_difference('Post.count', -1) do
delete :destroy, id: @post.to_param
end
assert_redirected_to posts_path
end
end

View File

@ -0,0 +1,4 @@
require 'test_helper'
class WelcomeHelperTest < ActionView::TestCase
end

View File

@ -3,6 +3,8 @@ require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests

View File

@ -1,4 +0,0 @@
require 'test_helper'
class HomeHelperTest < ActionView::TestCase
end