A Z Introduction To Ruby On Rails
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

A Z Introduction To Ruby On Rails

on

  • 2,619 views

 

Statistics

Views

Total Views
2,619
Views on SlideShare
2,611
Embed Views
8

Actions

Likes
2
Downloads
48
Comments
0

1 Embed 8

http://www.slideshare.net 8

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

A Z Introduction To Ruby On Rails Presentation Transcript

  • 1. Hello
  • 2. Robert Dempsey
  • 3. adsdevshop.com
  • 4. adsdevshop.com/apps
  • 5. rdempsey
  • 6. A-Z Intro To Ruby on Rails
  • 7. Always Be Learning
  • 8. Teach Others
  • 9. Be Bold
  • 10. Whatʼs Going On • Introduction to Ruby • Introduction to Rails • Hands-on Lab • Breaks interspersed
  • 11. Ruby
  • 12. 1995
  • 13. 2006
  • 14. Perl SmallTalk Eiffel Ada + Lisp Ruby
  • 15. #10 baby!
  • 16. 5.times { print “We love Ruby” }
  • 17. class Numeric def plus(x) self.+(x) end end y = 5.plus 6 # y is now equal to 11
  • 18. Rails
  • 19. 2005
  • 20. Image copyright 2008 Yoshiko314 (Flickr)
  • 21. MVC
  • 22. Model View Controller
  • 23. Model View Controller
  • 24. Model View Controller
  • 25. Model View Controller
  • 26. map.root :controller => ʻemployersʼ
  • 27. 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
  • 28. Letʼs Build Something
  • 29. The Project • Employers • Jobs
  • 30. rails jobby -d postgresql
  • 31. rails jobby -d mysql
  • 32. README Rakefile app/ config/ db/ doc/ lib/ log/ public/ script/ test/ tmp/ vendor/
  • 33. README Rakefile app/ config/ db/ doc/ lib/ log/ public/ script/ test/ tmp/ vendor/
  • 34. README Rakefile app/ config/ db/ doc/ lib/ log/ public/ script/ test/ tmp/ vendor/
  • 35. README Rakefile app/ config/ db/ doc/ lib/ log/ public/ script/ test/ tmp/ vendor/
  • 36. app/ controllers/ helpers/ models/ views/
  • 37. README Rakefile app/ config/ db/ doc/ lib/ log/ public/ script/ test/ tmp/ vendor/
  • 38. README Rakefile app/ config/ db/ doc/ lib/ log/ public/ script/ test/ tmp/ vendor/
  • 39. README Rakefile app/ config/ db/ doc/ lib/ log/ public/ script/ test/ tmp/ vendor/
  • 40. README Rakefile app/ config/ db/ doc/ lib/ log/ public/ script/ test/ tmp/ vendor/
  • 41. README Rakefile app/ config/ db/ doc/ lib/ log/ public/ script/ test/ tmp/ vendor/
  • 42. README Rakefile app/ config/ db/ doc/ lib/ log/ public/ script/ test/ tmp/ vendor/
  • 43. README Rakefile app/ config/ db/ doc/ lib/ log/ public/ script/ test/ tmp/ vendor/
  • 44. README Rakefile app/ config/ db/ doc/ lib/ log/ public/ script/ test/ tmp/ vendor/
  • 45. README Rakefile app/ config/ db/ doc/ lib/ log/ public/ script/ test/ tmp/ vendor/
  • 46. README Rakefile app/ config/ db/ doc/ lib/ log/ public/ script/ test/ tmp/ vendor/
  • 47. development: adapter: postgresql encoding: unicode database: jobby_development pool: 5 username: root password: config/database.yml
  • 48. development: adapter: mysql encoding: utf8 database: jobby_development pool: 5 username: root password: socket: /tmp/mysql.sock config/database.yml
  • 49. rake db:create
  • 50. script/server
  • 51. rm public/index.html
  • 52. map.connect ʻ:controller/:action/:idʼ map.connect ʻ:controller/:action/:id.:formatʼ config/routes.rb
  • 53. script/generate model Employer
  • 54. script/generate scaffold Employer name:string address:string city:string state:string zipcode:string
  • 55. exists app/models/ exists app/controllers/ exists app/helpers/ create app/views/employers exists app/views/layouts/ exists test/functional/ exists test/unit/ create test/unit/helpers/ exists public/stylesheets/ create app/views/employers/index.html.erb create app/views/employers/show.html.erb create app/views/employers/new.html.erb create app/views/employers/edit.html.erb create app/views/layouts/employers.html.erb create public/stylesheets/scaffold.css create app/controllers/employers_controller.rb create test/functional/employers_controller_test.rb create app/helpers/employers_helper.rb create test/unit/helpers/employers_helper_test.rb route map.resources :employers dependency model exists app/models/ exists test/unit/ exists test/fixtures/ create app/models/employer.rb create test/unit/employer_test.rb create test/fixtures/employers.yml create db/migrate create db/migrate/20090501175821_create_employers.rb
  • 56. app/models/employer.rb db/migrate/20090...1_create_employers.rb app/views/employers/index.html.erb app/views/employers/show.html.erb app/views/employers/new.html.erb app/views/employers/edit.html.erb app/views/layouts/employers.html.erb public/stylesheets/scaffold.css app/controllers/employers_controller.rb app/helpers/employers_helper.rb test/functional/employers_controller_test.rb test/unit/helpers/employers_helper_test.rb test/unit/employer_test.rb test/fixtures/employers.yml route map.resources :employers
  • 57. class CreateEmployers < ActiveRecord::Migration def self.up create_table :employers do |t| t.string :name t.string :address t.string :city t.string :state t.string :zipcode t.timestamps end end def self.down drop_table :employers end end db/migrations/2009...create_employers.rb
  • 58. rake db:migrate
  • 59. map.root :controller => ʻemployersʼ config/routes.rb
  • 60. http://localhost:3000/
  • 61. class Employer < ActiveRecord::Base end app/models/employer.rb
  • 62. class Employer < ActiveRecord::Base validates_presence_of :name validates_length_of :city, :minimum => 3 end app/models/employer.rb
  • 63. http://localhost:3000/employers/new
  • 64. Controller => CRUD
  • 65. Model => Logic
  • 66. script/console
  • 67. # 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
  • 68. # 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
  • 69. # 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
  • 70. app/views/employers/index.html.erb
  • 71. <%=h employer.name %> app/views/employers/index.html.erb
  • 72. <%= link_to 'New employer', ... %> app/views/employers/index.html.erb
  • 73. edit_employer_path(employer) app/views/employers/index.html.erb
  • 74. app/views/layouts/employers.html.erb
  • 75. # 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
  • 76. app/views/employers/new.html.erb
  • 77. # 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
  • 78. # 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
  • 79. app/views/employers/show.html.erb
  • 80. # GET /employers/1/edit def edit @employer = Employer.find(params[:id]) end app/controllers/employers_controller.rb
  • 81. app/views/employers/edit.html.erb
  • 82. # PUT /employers/1 # PUT /employers/1.xml def update @employer = Employer.find(params[:id]) respond_to do |format| if @employer.update_attributes(params[:employer]) flash[:notice] = 'Employer was successfully updated.' format.html { redirect_to(@employer) } format.xml { head :ok } else format.html { render :action => quot;editquot; } format.xml { render :xml => @employer.errors, :status => :unprocessable_entity } end end end app/controllers/employers_controller.rb
  • 83. # DELETE /employers/1 # DELETE /employers/1.xml def destroy @employer = Employer.find(params[:id]) @employer.destroy respond_to do |format| format.html { redirect_to(employers_url) } format.xml { head :ok } end end app/controllers/employers_controller.rb
  • 84. Donʼt Repeat Yourself
  • 85. Partials
  • 86. _form.html.erb app/views/employers/_form.html.erb
  • 87. app/views/employers/_form.html.erb
  • 88. <%= render :partial => 'form' %>
  • 89. app/views/employers/new.html.erb
  • 90. app/views/employers/edit.html.erb
  • 91. <%= render :partial => 'shared/form' %>
  • 92. @employer = Employer.find(params[:id]) app/controllers/employers_controller.erb
  • 93. before_filter :find_employer, :only => [:show, :edit, :update, :destroy] app/controllers/employers_controller.rb
  • 94. app/controllers/employers_controller.rb
  • 95. private def find_employer @employer = Employer.find(params[:id]) end app/controllers/employers_controller.rb
  • 96. app/controllers/employers_controller.rb
  • 97. script/generate scaffold Job name:string description:text
  • 98. 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
  • 99. rake db:migrate
  • 100. belongs_to has_one has_many has_many :through has_one :through has_and_belongs_to_many
  • 101. 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
  • 102. 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
  • 103. 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
  • 104. 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
  • 105. physicians Model: Physician has_and_belongs_to_many :patients id integer physicians_patients name string physician_id integer patients patient_id integer Model: Patient has_and_belongs_to_many :physicians id integer name string
  • 106. class Job < ActiveRecord::Base end app/models/job.rb
  • 107. class Job < ActiveRecord::Base belongs_to :employer validates_presence_of :name validates_presence_of :description end app/models/job.rb
  • 108. class Job < ActiveRecord::Base belongs_to :employer validates_presence_of :name validates_presence_of :description end app/models/job.rb
  • 109. class Employer < ActiveRecord::Base has_many :jobs validates_presence_of :name validates_length_of :city, :minimum => 3 end app/models/employer.rb
  • 110. map.resources :employers map.resources :employers, :has_many => :jobs map.resources :jobs app/controllers/employers_controller.rb
  • 111. before_filter :find_employer app/controllers/jobs_controller.rb
  • 112. app/controllers/jobs_controller.rb
  • 113. private def find_employer @employer = Employer.find(params[:employer_id]) end app/controllers/jobs_controller.rb
  • 114. app/controllers/jobs_controller.rb
  • 115. # 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
  • 116. # 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
  • 117. @job = @employer.jobs.build
  • 118. app/views/jobs/index.html.erb
  • 119. # 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
  • 120. # 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
  • 121. app/views/jobs/show.html.erb
  • 122. # GET /jobs/1/edit def edit @job = @employer.jobs.find(params[:id]) end app/controllers/jobs_controller.rb
  • 123. app/views/jobs/edit.html.erb
  • 124. # PUT /jobs/1 # PUT /jobs/1.xml def update @job = Job.find(params[:id]) respond_to do |format| if @job.update_attributes(params[:job]) flash[:notice] = 'Job was successfully updated.' format.html { redirect_to employer_job_url(@employer, @job) } format.xml { head :ok } else format.html { render :action => quot;editquot; } format.xml { render :xml => @job.errors, :status => :unprocessable_entity } end end end app/controllers/jobs_controller.rb
  • 125. # DELETE /jobs/1 # DELETE /jobs/1.xml def destroy @job = Job.find(params[:id]) @job.destroy respond_to do |format| format.html { redirect_to employer_jobs_url(@employer) } format.xml { head :ok } end end app/controllers/jobs_controller.rb
  • 126. app/views/employers/show.html.erb
  • 127. app/views/jobs/index.html.erb
  • 128. app/views/jobs/index.html.erb
  • 129. app/views/employers/index.html.erb
  • 130. app/views/employers/index.html.erb
  • 131. app/controllers/employers_controller.rb
  • 132. app/controllers/employers_controller.rb
  • 133. app/views/employers/index.html.erb
  • 134. app/views/employers/index.html.erb
  • 135. app/controllers/jobs_controller.rb
  • 136. app/controllers/employers_controller.rb
  • 137. app/views/employers/index.html.erb
  • 138. Next Steps
  • 139. DRY up our Job views Add search to our jobs Add a logins for employers Add tags to our jobs
  • 140. Resources
  • 141. Rails Guides Agile Web Development (PP) Intro to Ruby 1.9 Cucumber + RSpec
  • 142. Contest!
  • 143. Letʼs Chat