Friday, October 30, 2009

Workshop 6

Study Guide

Study Guide Study Guide
Enjoying the Ride: Web framework alternatives, scalability and flexibility

Study GuideDEVELOPER’S THREAD (RED team)

Along with auto-generated application controller, controllers were created for passengers, site, and user:




The site contorller is used to for the static view templates such as the Home, About Us and Help.
The user controller is used for the registration views. It is the following actions:
  • index,
  • edit,
  • register,
  • login, and
  • logout.
The passengers controller is used by the CRUD operations generated through the scaffolding process. Even though I’ve left the original scaffold generated model, controller and views I’ve had to make modifications so that the users table is linked to the passengers table. The passengers controller has the following methods:
  • index,
  • show,
  • new,
  • create,
  • update, and
  • destroy.
Here is the code for the passengers controller:
class PassengersController < ApplicationController # GET /passengers # GET /passengers.xml def index @user = User.find(session[:user_id]) @passengers = Passenger.find_all_by_user_id(session[:user_id]) respond_to do |format| format.html # index.html.erb format.xml { render :xml => @passengers }
end
end

# GET /passengers/1
# GET /passengers/1.xml
def show
@passenger = Passenger.find(params[:id])
if @passenger.user_id == User.find(session[:user_id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @passenger }
end
end
end
# GET /passengers/new
# GET /passengers/new.xml
def new
@user = User.find(session[:user_id])
@passenger = Passenger.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @passenger }
end
end
# GET /passengers/1/edit
def edit
@passenger = Passenger.find(params[:id])
end
# POST /passengers
# POST /passengers.xml
def create
@passenger = Passenger.new(params[:passenger])
@passenger.user_id = session[:user_id]
respond_to do |format|
if @passenger.save
flash[:notice] = ‘Passenger was successfully created.’
format.html { redirect_to(@passenger) }
format.xml { render :xml => @passenger, :status => :created, :location => @passenger }
else
format.html { render :action => “new” }
format.xml { render :xml => @passenger.errors, :status => :unprocessable_entity }
end
end
end
# PUT /passengers/1
# PUT /passengers/1.xml
def update
@passenger = Passenger.find(params[:id])
respond_to do |format|
if @passenger.update_attributes(params[:passenger])
flash[:notice] = ‘Passenger was successfully updated.’
format.html { redirect_to(@passenger) }
format.xml { head :ok }
else
format.html { render :action => “edit” }
format.xml { render :xml => @passenger.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /passengers/1
# DELETE /passengers/1.xml
def destroy
@passenger = Passenger.find(params[:id])
@passenger.destroy
respond_to do |format|
format.html { redirect_to(passengers_url) }
format.xml { head :ok }
end
end
end
In the first method, index, is where the main adjustment has been made the line:
@passengers = Passenger.find_all_by_user_id(session[:user_id])
Passes the user_id which is the foreign key to the passengers to do the lookup for all entries whith that user_id.
The other notable additions to the code occur in the model, the following line is added to the user model:

class User < ActiveRecord::Base
has_many :passengers
.
.
.

And to the passenger model:

class Passenger < ActiveRecord::Base
belongs_to :user
end

No comments:

Post a Comment