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

Commit

Permalink
adding episode 185
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Oct 26, 2009
1 parent e79a6ab commit b09f740
Show file tree
Hide file tree
Showing 103 changed files with 9,318 additions and 0 deletions.
9 changes: 9 additions & 0 deletions episode-185/README
@@ -0,0 +1,9 @@
Railscasts Episode #185: Formtastic Part 2

http://railscasts.com/episodes/185

Commands

script/generate nifty_scaffold problem name:string
rake db:migrate
script/generate nifty_scaffold symptom animal_id:integer problem_id:integer --skip-controller
3 changes: 3 additions & 0 deletions episode-185/vet/.gitignore
@@ -0,0 +1,3 @@
log/*.log
tmp/**/*
*.sqlite3
1 change: 1 addition & 0 deletions episode-185/vet/README
@@ -0,0 +1 @@
Example application for Railscasts episode.
10 changes: 10 additions & 0 deletions episode-185/vet/Rakefile
@@ -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'
44 changes: 44 additions & 0 deletions episode-185/vet/app/controllers/animals_controller.rb
@@ -0,0 +1,44 @@
class AnimalsController < ApplicationController
def index
@animals = Animal.all
end

def show
@animal = Animal.find(params[:id])
end

def new
@animal = Animal.new
end

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

def edit
@animal = Animal.find(params[:id])
end

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

def destroy
@animal = Animal.find(params[:id])
@animal.destroy
flash[:notice] = "Successfully destroyed animal."
redirect_to animals_url
end
end
10 changes: 10 additions & 0 deletions episode-185/vet/app/controllers/application_controller.rb
@@ -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
44 changes: 44 additions & 0 deletions episode-185/vet/app/controllers/categories_controller.rb
@@ -0,0 +1,44 @@
class CategoriesController < ApplicationController
def index
@categories = Category.all
end

def show
@category = Category.find(params[:id])
end

def new
@category = Category.new
end

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

def edit
@category = Category.find(params[:id])
end

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

def destroy
@category = Category.find(params[:id])
@category.destroy
flash[:notice] = "Successfully destroyed category."
redirect_to categories_url
end
end
44 changes: 44 additions & 0 deletions episode-185/vet/app/controllers/problems_controller.rb
@@ -0,0 +1,44 @@
class ProblemsController < ApplicationController
def index
@problems = Problem.all
end

def show
@problem = Problem.find(params[:id])
end

def new
@problem = Problem.new
end

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

def edit
@problem = Problem.find(params[:id])
end

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

def destroy
@problem = Problem.find(params[:id])
@problem.destroy
flash[:notice] = "Successfully destroyed problem."
redirect_to problems_url
end
end
2 changes: 2 additions & 0 deletions episode-185/vet/app/helpers/animals_helper.rb
@@ -0,0 +1,2 @@
module AnimalsHelper
end
3 changes: 3 additions & 0 deletions episode-185/vet/app/helpers/application_helper.rb
@@ -0,0 +1,3 @@
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
end
2 changes: 2 additions & 0 deletions episode-185/vet/app/helpers/categories_helper.rb
@@ -0,0 +1,2 @@
module CategoriesHelper
end
22 changes: 22 additions & 0 deletions episode-185/vet/app/helpers/layout_helper.rb
@@ -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-185/vet/app/helpers/problems_helper.rb
@@ -0,0 +1,2 @@
module ProblemsHelper
end
7 changes: 7 additions & 0 deletions episode-185/vet/app/models/animal.rb
@@ -0,0 +1,7 @@
class Animal < ActiveRecord::Base
attr_accessible :name, :category_id, :born_on, :female, :problem_ids
belongs_to :category
has_many :symptoms
has_many :problems, :through => :symptoms
validates_presence_of :name, :born_on
end
4 changes: 4 additions & 0 deletions episode-185/vet/app/models/category.rb
@@ -0,0 +1,4 @@
class Category < ActiveRecord::Base
attr_accessible :name, :description
has_many :animals
end
5 changes: 5 additions & 0 deletions episode-185/vet/app/models/problem.rb
@@ -0,0 +1,5 @@
class Problem < ActiveRecord::Base
attr_accessible :name
has_many :symptoms
has_many :animals, :through => :symptoms
end
5 changes: 5 additions & 0 deletions episode-185/vet/app/models/symptom.rb
@@ -0,0 +1,5 @@
class Symptom < ActiveRecord::Base
attr_accessible :animal_id, :problem_id
belongs_to :animial
belongs_to :problem
end
11 changes: 11 additions & 0 deletions episode-185/vet/app/views/animals/_form.html.erb
@@ -0,0 +1,11 @@
<% semantic_form_for @animal do |f| %>
<%= f.error_messages %>
<% f.inputs do %>
<%= f.input :name, :hint => "Use the owner's name if none is provided" %>
<%= f.input :born_on, :start_year => 1900 %>
<%= f.input :category, :include_blank => false %>
<%= f.input :female, :as => :radio, :label => "Gender", :collection => [["Male", false], ["Female", true]] %>
<%= f.input :problems, :as => :check_boxes, :required => false %>
<% end %>
<%= f.buttons %>
<% end %>
8 changes: 8 additions & 0 deletions episode-185/vet/app/views/animals/edit.html.erb
@@ -0,0 +1,8 @@
<% title "Edit Animal" %>
<%= render :partial => 'form' %>

<p>
<%= link_to "Show", @animal %> |
<%= link_to "View All", animals_path %>
</p>
23 changes: 23 additions & 0 deletions episode-185/vet/app/views/animals/index.html.erb
@@ -0,0 +1,23 @@
<% title "Animals" %>

<table>
<tr>
<th>Name</th>
<th>Category</th>
<th>Born On</th>
<th>Female</th>
</tr>
<% for animal in @animals %>
<tr>
<td><%=h animal.name %></td>
<td><%=h animal.category_id %></td>
<td><%=h animal.born_on %></td>
<td><%=h animal.female %></td>
<td><%= link_to "Show", animal %></td>
<td><%= link_to "Edit", edit_animal_path(animal) %></td>
<td><%= link_to "Destroy", animal, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>

<p><%= link_to "New Animal", new_animal_path %></p>
5 changes: 5 additions & 0 deletions episode-185/vet/app/views/animals/new.html.erb
@@ -0,0 +1,5 @@
<% title "New Animal" %>
<%= render :partial => 'form' %>

<p><%= link_to "Back to List", animals_path %></p>
24 changes: 24 additions & 0 deletions episode-185/vet/app/views/animals/show.html.erb
@@ -0,0 +1,24 @@
<% title "Animal" %>

<p>
<strong>Name:</strong>
<%=h @animal.name %>
</p>
<p>
<strong>Category:</strong>
<%=h @animal.category_id %>
</p>
<p>
<strong>Born On:</strong>
<%=h @animal.born_on %>
</p>
<p>
<strong>Female:</strong>
<%=h @animal.female %>
</p>

<p>
<%= link_to "Edit", edit_animal_path(@animal) %> |
<%= link_to "Destroy", @animal, :confirm => 'Are you sure?', :method => :delete %> |
<%= link_to "View All", animals_path %>
</p>
4 changes: 4 additions & 0 deletions episode-185/vet/app/views/categories/_form.html.erb
@@ -0,0 +1,4 @@
<% semantic_form_for @category do |f| %>
<%= f.inputs %>
<%= f.buttons %>
<% end %>
8 changes: 8 additions & 0 deletions episode-185/vet/app/views/categories/edit.html.erb
@@ -0,0 +1,8 @@
<% title "Edit Category" %>
<%= render :partial => 'form' %>

<p>
<%= link_to "Show", @category %> |
<%= link_to "View All", categories_path %>
</p>
19 changes: 19 additions & 0 deletions episode-185/vet/app/views/categories/index.html.erb
@@ -0,0 +1,19 @@
<% title "Categories" %>

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

<p><%= link_to "New Category", new_category_path %></p>
5 changes: 5 additions & 0 deletions episode-185/vet/app/views/categories/new.html.erb
@@ -0,0 +1,5 @@
<% title "New Category" %>
<%= render :partial => 'form' %>

<p><%= link_to "Back to List", categories_path %></p>
16 changes: 16 additions & 0 deletions episode-185/vet/app/views/categories/show.html.erb
@@ -0,0 +1,16 @@
<% title "Category" %>

<p>
<strong>Name:</strong>
<%=h @category.name %>
</p>
<p>
<strong>Description:</strong>
<%=h @category.description %>
</p>

<p>
<%= link_to "Edit", edit_category_path(@category) %> |
<%= link_to "Destroy", @category, :confirm => 'Are you sure?', :method => :delete %> |
<%= link_to "View All", categories_path %>
</p>
22 changes: 22 additions & 0 deletions episode-185/vet/app/views/layouts/application.html.erb
@@ -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 xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title><%= h(yield(:title) || "Untitled") %></title>
<%= stylesheet_link_tag 'application', 'formtastic', 'formtastic_changes', :cache => "base" %>
<%= 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>
8 changes: 8 additions & 0 deletions episode-185/vet/app/views/problems/_form.html.erb
@@ -0,0 +1,8 @@
<% form_for @problem do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p><%= f.submit "Submit" %></p>
<% end %>

0 comments on commit b09f740

Please sign in to comment.