*** Originally based on O'Reilly Tutorial created in 2005 for Ruby on Rails ****
/var/www/myapp/cookbook/config/routes.rb
routes.rb **
ActionController::Routing::Routes.draw do |map|
map.resources :recipes
map.resources :categories, :has_many => :recipes # This line took me a week to figure out !!!!!
# The priority is based upon order of creation: first created -> highest priority.
# Sample of regular route:
# map.connect 'products/:id', :controller => 'catalog', :action => 'view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
# map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase'
# This route can be invoked with purchase_url(:id => product.id)
# Sample resource route (maps HTTP verbs to controller actions automatically):
# map.resources :products
# Sample resource route with options:
# map.resources :products, :member => { :short => :get, :toggle => :post }, :collection => { :sold => :get }
# Sample resource route with sub-resources:
# map.resources :products, :has_many => [ :comments, :sales ], :has_one => :seller
# Sample resource route with more complex sub-resources
# map.resources :products do |products|
# products.resources :comments
# products.resources :sales, :collection => { :recent => :get }
# end
# Sample resource route within a namespace:
# map.namespace :admin do |admin|
# # Directs /admin/products/* to Admin::ProductsController (app/controllers/admin/products_controller.rb)
# admin.resources :products
# end
# You can have the root of your site routed with map.root -- just remember to delete public/index.html.
# map.root :controller => "welcome"
# See how all your routes lay out with "rake routes"
# Install the default routes as the lowest priority.
# Note: These default routes make all actions in every controller accessible via GET requests. You should
# consider removing or commenting them out if you're using named routes and resources.
map.root :controller => "recipes"
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
****************************************************
/var/www/myapp/cookbook/app/models files
recipe.rb **
class Recipe < ActiveRecord::Base
belongs_to :category
end
category.rb **
class Category < ActiveRecord::Base
has_many :recipes
end
*****************************************************
/var/www/myapp/cookbook/app/controllers
categories_controller.rb **
class CategoriesController < ApplicationController
# GET /categories
# GET /categories.xml
def index
@categories = Category.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @categories }
end
end
# GET /categories/1
# GET /categories/1.xml
def show
@category = Category.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @category }
end
end
# GET /categories/new
# GET /categories/new.xml
def new
@category = Category.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @category }
end
end
# GET /categories/1/edit
def edit
@category = Category.find(params[:id])
end
# POST /categories
# POST /categories.xml
def create
@category = Category.new(params[:category])
respond_to do |format|
if @category.save
flash[:notice] = 'Category was successfully created.'
format.html { redirect_to(@category) }
format.xml { render :xml => @category, :status => :created, :location => @category }
else
format.html { render :action => "new" }
format.xml { render :xml => @category.errors, :status => :unprocessable_entity }
end
end
end
# PUT /categories/1
# PUT /categories/1.xml
def update
@category = Category.find(params[:id])
respond_to do |format|
if @category.update_attributes(params[:category])
flash[:notice] = 'Category was successfully updated.'
format.html { redirect_to(@category) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @category.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /categories/1
# DELETE /categories/1.xml
def destroy
@category = Category.find(params[:id])
@category.destroy
respond_to do |format|
format.html { redirect_to(categories_url) }
format.xml { head :ok }
end
end
end
recipes_controller.rb **
class RecipesController < ApplicationController
# GET /recipes
# GET /recipes.xml
def index
@recipes = Recipe.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @recipes }
end
end
# GET /recipes/1
# GET /recipes/1.xml
def show
@recipe = Recipe.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @recipe }
end
end
# GET /recipes/new
# GET /recipes/new.xml
def new
@recipe = Recipe.new
@categories = Category.all
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @recipe }
end
end
# GET /recipes/1/edit
def edit
@recipe = Recipe.find(params[:id])
@categories = Category.all
end
# POST /recipes
# POST /recipes.xml
def create
@recipe = Recipe.new(params[:recipe])
respond_to do |format|
if @recipe.save
flash[:notice] = 'Recipe was successfully created.'
format.html { redirect_to(@recipe) }
format.xml { render :xml => @recipe, :status => :created, :location => @recipe }
else
format.html { render :action => "new" }
format.xml { render :xml => @recipe.errors, :status => :unprocessable_entity }
end
end
end
# PUT /recipes/1
# PUT /recipes/1.xml
def update
@recipe = Recipe.find(params[:id])
respond_to do |format|
if @recipe.update_attributes(params[:recipe])
flash[:notice] = 'Recipe was successfully updated.'
format.html { redirect_to(@recipe) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @recipe.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /recipes/1
# DELETE /recipes/1.xml
def destroy
@recipe = Recipe.find(params[:id])
@recipe.destroy
respond_to do |format|
format.html { redirect_to(recipes_url) }
format.xml { head :ok }
end
end
end
my_test_controller.rb **
class MyTestController < ApplicationController
def index
end
end
application_controller.rb **
# 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
*****************************************************
/var/www/myapp/cookbook/app/views/my_test file:
index.html.erb **
Testing and Updating O'Reilly tutorial
*****************************************************
/var/www/myapp/cookbook/app/views/recipes files:
index.html.erb **
All Recipes
Online Cookbook - All Recipes
Recipe |
Category |
Date |
| <%= link_to recipe.title, :action => "show", :id => recipe.id %> | <%= recipe.category.name %> | <%= recipe.date %> |
<%= link_to "Create new recipe", :action => "new" %>
<%= link_to 'Create New category', new_category_path %>
edit.html.erb **ID not displayed because it is a protected field.
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :description %>
<%= f.text_field :description %>
<%= f.label :date %>
<%= f.date_select :date %>
<%= f.label :instructions %>
<%= f.text_area :instructions %>
Select Category Name :
<%= f.submit 'Update' %>
<% end %> <%= link_to 'Show this Recipe ', @recipe %> | <%= link_to 'Back to Recipes ', recipes_path %> new.html.erb *** This one took three days to figure out!ID is not displayed because it will be auto-generated and is a protected field.
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :description %>
<%= f.text_field :description %>
<%= f.label :date %>
<%= f.date_select :date %>
<%= f.label :instructions %>
<%= f.text_area :instructions %>
Select Category Name :
<%= f.submit 'Create' %>
<% end %> <%= link_to 'Back to Recipes', recipes_path %> show.html.erb **Id: <%=h @recipe.id %>
Title: <%=h @recipe.title %>
Description: <%=h @recipe.description %>
Date: <%=h @recipe.date %>
Instructions: <%=h @recipe.instructions %>
Category Name: <%=h @recipe.category.name %>
<%= link_to 'Edit Recipe', edit_recipe_path(@recipe) %> | <%= link_to 'Back to Recipes ', recipes_path %> /var/www/myapp/cookbook/app/views/categories files: index.html.erb **| Id | Name | |||
|---|---|---|---|---|
| <%=h category.id %> | <%=h category.name %> | <%= link_to 'Show', category %> | <%= link_to 'Edit', edit_category_path(category) %> | <%= link_to 'Destroy', category, :confirm => 'Are you sure?', :method => :delete %> |
<%= link_to 'Back to Recipes ', recipes_path %>
edit.html.erb **Primary Key ID not displayed because it is a protected field.
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.submit 'Update' %>
<% end %> <%= link_to 'Show', @category %> | <%= link_to 'Back', categories_path %> new.html.erb ***Note, Primary key is not displayed because it is a protected field.
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.submit 'Create' %>
<% end %><%= link_to 'Back to categories', categories_path %>
<%= link_to 'Back to Recipes List ', recipes_path %>
show.html.erbId: <%=h @category.id %>
Name: <%=h @category.name %>
<%= link_to 'Edit', edit_category_path(@category) %> | <%= link_to 'Back', categories_path %>