The document discusses an introduction to Ruby on Rails. It provides an overview of Ruby and Rails, demonstrates how to build a sample application using Rails with employers and jobs, and discusses concepts like MVC architecture, scaffolding, associations, and RESTful routing. The sample application allows users to view, create, edit and delete employer and job records along with their associations.
39. Action HTTP Method URL XML
index GET /jobs /jobs.xml
show GET /jobs/1 /jobs/1.xml
new GET /jobs/new
edit GET /jobs/1;edit
create POST /jobs /jobs.xml
update PUT /jobs/1 /jobs/1.xml
destroy DELETE /jobs/1 /jobs/1.xml
82. # GET /employers
# GET /employers.xml
def index
@employers = Employer.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @employers }
end
end
app/controllers/employers_controller.rb
83. # GET /employers
# GET /employers.xml
def index
@employers = Employer.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @employers }
end
end
app/controllers/employers_controller.rb
84. # GET /employers
# GET /employers.xml
def index
@employers = Employer.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml =>
@employers }
end
end
app/controllers/employers_controller.rb
90. # GET /employers/new
# GET /employers/new.xml
def new
@employer = Employer.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @employer }
end
end
app/controllers/employers_controller.rb
92. # POST /employers
# POST /employers.xml
def create
@employer = Employer.new(params[:employer])
respond_to do |format|
if @employer.save
flash[:notice] = 'Employer was successfully created.'
format.html { redirect_to(@employer) }
format.xml { render :xml => @employer,
:status => :created, :location => @employer }
else
format.html { render :action => quot;newquot; }
format.xml { render :xml => @employer.errors, :status
=> :unprocessable_entity }
end
end
end
app/controllers/employers_controller.rb
93. # GET /employers/1
# GET /employers/1.xml
def show
@employer = Employer.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @employer }
end
end
app/controllers/employers_controller.rb
113. class CreateJobs < ActiveRecord::Migration
def self.up
create_table :jobs do |t|
t.integer :employer_id
t.string :name
t.text :description
t.timestamps
end
end
def self.down
drop_table :jobs
end
end
db/migrations/2009...create_jobs.rb
116. jobs employers
Model: Job Model: Employer
belongs_to :employer has_many :jobs
id integer id integer
employer_id integer name string
name string address string
117. employers jobs
Model: Employer Model: Job
has_one :job belongs_to :employer
id integer id integer
name string employer_id integer
address string name string
118. employers jobs
Model: Employer Model: Job
has_many :jobs belongs_to :employer
id integer id integer
name string employer_id integer
address string name string
119. physicians
Model: Physician
has_many :appointments
has_many :patients,
:through => :appointments
appointments
id integer
Model: Appointment
name string belongs_to :physician
belongs_to :patient
id integer
patients
physician_id integer
Model: Patient
patient_id integer
has_many :appointments
has_many :physicians,
:through => :appointments
id integer
name string
131. # GET /jobs
# GET /jobs.xml
def index
@jobs = @employer.jobs
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @jobs }
end
end
app/controllers/jobs_controller.rb
132.
133. # GET /jobs/new
# GET /jobs/new.xml
def new
@job = Job.new
@job = @employer.jobs.build
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @job }
end
end
app/controllers/jobs_controller.rb
136. # POST /jobs
# POST /jobs.xml
def create
@employer = Employer.find(params[:employer_id])
@job = @employer.jobs.build(params[:job])
respond_to do |format|
if @job.save
flash[:notice] = 'Job was successfully created.'
format.html { redirect_to employer_job_url(@employer, @job) }
format.xml { render :xml => @job, :status => :created, :location => @job }
else
format.html { render :action => quot;newquot; }
format.xml { render :xml => @job.errors, :status => :unprocessable_entity }
end
end
end
app/controllers/jobs_controller.rb
137. # GET /jobs/1
# GET /jobs/1.xml
def show
@job = @employer.jobs.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @job }
end
end
app/controllers/jobs_controller.rb