Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
adding episode 196
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Jan 10, 2010
1 parent 4201617 commit 87d2559
Show file tree
Hide file tree
Showing 72 changed files with 8,365 additions and 0 deletions.
12 changes: 12 additions & 0 deletions episode-196/README
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,12 @@
Railscasts Episode #196: Nested Fields Part 1

http://railscasts.com/episodes/196

Commands

rails surveysays
script/generate nifty_layout
script/generate nifty_scaffold survey name:string
script/generate model question survey_id:integer content:text
script/generate model answer question_id:integer content:string
rake db:migrate
3 changes: 3 additions & 0 deletions episode-196/surveysays/.gitignore
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,3 @@
tmp/**/*
log/*.log
*.sqlite3
4 changes: 4 additions & 0 deletions episode-196/surveysays/README
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,4 @@
Example application for Railscasts. To get started:

rake db:migrate
script/server
10 changes: 10 additions & 0 deletions episode-196/surveysays/Rakefile
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,10 @@
# 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.

require(File.join(File.dirname(__FILE__), 'config', 'boot'))

require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

require 'tasks/rails'
10 changes: 10 additions & 0 deletions episode-196/surveysays/app/controllers/application_controller.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,10 @@
# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection for details

# Scrub sensitive parameters from your log
# filter_parameter_logging :password
end
48 changes: 48 additions & 0 deletions episode-196/surveysays/app/controllers/surveys_controller.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,48 @@
class SurveysController < ApplicationController
def index
@surveys = Survey.all
end

def show
@survey = Survey.find(params[:id])
end

def new
@survey = Survey.new
3.times do
question = @survey.questions.build
4.times { question.answers.build }
end
end

def create
@survey = Survey.new(params[:survey])
if @survey.save
flash[:notice] = "Successfully created survey."
redirect_to @survey
else
render :action => 'new'
end
end

def edit
@survey = Survey.find(params[:id])
end

def update
@survey = Survey.find(params[:id])
if @survey.update_attributes(params[:survey])
flash[:notice] = "Successfully updated survey."
redirect_to @survey
else
render :action => 'edit'
end
end

def destroy
@survey = Survey.find(params[:id])
@survey.destroy
flash[:notice] = "Successfully destroyed survey."
redirect_to surveys_url
end
end
3 changes: 3 additions & 0 deletions episode-196/surveysays/app/helpers/application_helper.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,3 @@
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
end
22 changes: 22 additions & 0 deletions episode-196/surveysays/app/helpers/layout_helper.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,22 @@
# These helper methods can be called in your template to set variables to be used in the layout
# This module should be included in all views globally,
# to do so you may need to add this line to your ApplicationController
# helper :layout
module LayoutHelper
def title(page_title, show_title = true)
@content_for_title = page_title.to_s
@show_title = show_title
end

def show_title?
@show_title
end

def stylesheet(*args)
content_for(:head) { stylesheet_link_tag(*args) }
end

def javascript(*args)
content_for(:head) { javascript_include_tag(*args) }
end
end
2 changes: 2 additions & 0 deletions episode-196/surveysays/app/helpers/surveys_helper.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,2 @@
module SurveysHelper
end
3 changes: 3 additions & 0 deletions episode-196/surveysays/app/models/answer.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,3 @@
class Answer < ActiveRecord::Base
belongs_to :question
end
5 changes: 5 additions & 0 deletions episode-196/surveysays/app/models/question.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
class Question < ActiveRecord::Base
belongs_to :survey
has_many :answers, :dependent => :destroy
accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end
4 changes: 4 additions & 0 deletions episode-196/surveysays/app/models/survey.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,4 @@
class Survey < ActiveRecord::Base
has_many :questions, :dependent => :destroy
accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end
22 changes: 22 additions & 0 deletions episode-196/surveysays/app/views/layouts/application.html.erb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title><%= h(yield(:title) || "Untitled") %></title>
<%= stylesheet_link_tag 'application' %>
<%= yield(:head) %>
</head>
<body>
<div id="container">
<%- flash.each do |name, msg| -%>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<%- end -%>
<%- if show_title? -%>
<h1><%=h yield(:title) %></h1>
<%- end -%>
<%= yield %>
</div>
</body>
</html>
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,6 @@
<p>
<%= f.label :content, "Answer" %>
<%= f.text_field :content %>
<%= f.check_box :_destroy %>
<%= f.label :_destroy, "Remove" %>
</p>
11 changes: 11 additions & 0 deletions episode-196/surveysays/app/views/surveys/_form.html.erb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,11 @@
<% form_for @survey do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<% f.fields_for :questions do |builder| %>
<%= render "question_fields", :f => builder %>
<% end %>
<p><%= f.submit "Submit" %></p>
<% end %>
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,9 @@
<p>
<%= f.label :content, "Question" %><br />
<%= f.text_area :content, :rows => 3 %><br />
<%= f.check_box :_destroy %>
<%= f.label :_destroy, "Remove Question" %>
</p>
<% f.fields_for :answers do |builder| %>
<%= render 'answer_fields', :f => builder %>
<% end %>
8 changes: 8 additions & 0 deletions episode-196/surveysays/app/views/surveys/edit.html.erb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,8 @@
<% title "Edit Survey" %>
<%= render :partial => 'form' %>

<p>
<%= link_to "Show", @survey %> |
<%= link_to "View All", surveys_path %>
</p>
17 changes: 17 additions & 0 deletions episode-196/surveysays/app/views/surveys/index.html.erb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,17 @@
<% title "Surveys" %>

<table>
<tr>
<th>Name</th>
</tr>
<% for survey in @surveys %>
<tr>
<td><%=h survey.name %></td>
<td><%= link_to "Show", survey %></td>
<td><%= link_to "Edit", edit_survey_path(survey) %></td>
<td><%= link_to "Destroy", survey, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>

<p><%= link_to "New Survey", new_survey_path %></p>
5 changes: 5 additions & 0 deletions episode-196/surveysays/app/views/surveys/new.html.erb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
<% title "New Survey" %>
<%= render :partial => 'form' %>

<p><%= link_to "Back to List", surveys_path %></p>
25 changes: 25 additions & 0 deletions episode-196/surveysays/app/views/surveys/show.html.erb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,25 @@
<% title "Survey" %>

<p>
<strong>Name:</strong>
<%=h @survey.name %>
</p>

<ol>
<% for question in @survey.questions %>
<li>
<%=h question.content %>
<ul>
<% for answer in question.answers %>
<li><%=h answer.content %></li>
<% end %>
</ul>
</li>
<% end %>
</ol>

<p>
<%= link_to "Edit", edit_survey_path(@survey) %> |
<%= link_to "Destroy", @survey, :confirm => 'Are you sure?', :method => :delete %> |
<%= link_to "View All", surveys_path %>
</p>
110 changes: 110 additions & 0 deletions episode-196/surveysays/config/boot.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,110 @@
# Don't change this file!
# Configure your app in config/environment.rb and config/environments/*.rb

RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)

module Rails
class << self
def boot!
unless booted?
preinitialize
pick_boot.run
end
end

def booted?
defined? Rails::Initializer
end

def pick_boot
(vendor_rails? ? VendorBoot : GemBoot).new
end

def vendor_rails?
File.exist?("#{RAILS_ROOT}/vendor/rails")
end

def preinitialize
load(preinitializer_path) if File.exist?(preinitializer_path)
end

def preinitializer_path
"#{RAILS_ROOT}/config/preinitializer.rb"
end
end

class Boot
def run
load_initializer
Rails::Initializer.run(:set_load_path)
end
end

class VendorBoot < Boot
def load_initializer
require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
Rails::Initializer.run(:install_gem_spec_stubs)
Rails::GemDependency.add_frozen_gem_path
end
end

class GemBoot < Boot
def load_initializer
self.class.load_rubygems
load_rails_gem
require 'initializer'
end

def load_rails_gem
if version = self.class.gem_version
gem 'rails', version
else
gem 'rails'
end
rescue Gem::LoadError => load_error
$stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
exit 1
end

class << self
def rubygems_version
Gem::RubyGemsVersion rescue nil
end

def gem_version
if defined? RAILS_GEM_VERSION
RAILS_GEM_VERSION
elsif ENV.include?('RAILS_GEM_VERSION')
ENV['RAILS_GEM_VERSION']
else
parse_gem_version(read_environment_rb)
end
end

def load_rubygems
min_version = '1.3.2'
require 'rubygems'
unless rubygems_version >= min_version
$stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
exit 1
end

rescue LoadError
$stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
exit 1
end

def parse_gem_version(text)
$1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
end

private
def read_environment_rb
File.read("#{RAILS_ROOT}/config/environment.rb")
end
end
end
end

# All that for this:
Rails.boot!
22 changes: 22 additions & 0 deletions episode-196/surveysays/config/database.yml
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,22 @@
# SQLite version 3.x
# gem install sqlite3-ruby (not necessary on OS X Leopard)
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000

production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
Loading

0 comments on commit 87d2559

Please sign in to comment.