This repository has been archived by the owner on Dec 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 646
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
72 changed files
with
8,365 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,3 @@ | |||
tmp/**/* | |||
log/*.log | |||
*.sqlite3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
episode-196/surveysays/app/controllers/application_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
48
episode-196/surveysays/app/controllers/surveys_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,2 @@ | |||
module SurveysHelper | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
episode-196/surveysays/app/views/layouts/application.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
6 changes: 6 additions & 0 deletions
6
episode-196/surveysays/app/views/surveys/_answer_fields.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
9 changes: 9 additions & 0 deletions
9
episode-196/surveysays/app/views/surveys/_question_fields.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.