• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Rails 2010 Workshop
 

Rails 2010 Workshop

on

  • 1,004 views

I gave this talk at ITP @ NYU April 7, 2010.

I gave this talk at ITP @ NYU April 7, 2010.

Statistics

Views

Total Views
1,004
Views on SlideShare
1,002
Embed Views
2

Actions

Likes
1
Downloads
6
Comments
0

2 Embeds 2

http://www.slideshare.net 1
http://www.slashdocs.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

CC Attribution License

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
  • Ruby on Rails is a web development framework that makes building complex web applications much easier. Ruby is the language in which Rails is built.
  • http://itp.nyu.edu/~dmt321/bad-php.txt http://itp.nyu.edu/~dmt321/better-php.txt
  • Rails 3 is on the verge of being released – it’s already out in beta. Some of the syntax, especially in routing, will be changed in Rails 3. But the fundamental concepts are the same.
  • Comments
  • table names, primary key, location of files
  • EXAMPLES
  • You can created Named Routes as well – this creates the hello_url and hello_path helpers.
  • Adding parameter :name
  • More sane way to do it  Also notice that :action => “index” is optional
  • Convention over configuration – Rails knows where to look for views.
  • Convention over configuration – Rails knows where to look for layouts.
  • Removed render :text code
  • ORM is Object-Relational Mapping. It converts a row in a database table to an object.
  • This is a lie. In fact Rails would not build a query like this.
  • There are three different environments (more are possible). We will work primarily with the “development” environment.
  • Notice the fields that were not in the migration file id - more convention over configuration
  • Chained scopes
  • Convention over configuration – looks for itp_student.advisor_id and classes.itp_student_id

Rails 2010 Workshop Rails 2010 Workshop Presentation Transcript

  • Ruby on Rails Workshop NYU ITP 2010 Presented by Daniel Tsadok
  • What is Ruby on Rails?
  • Why Ruby on Rails? Why not PHP?
  • Organization
  • Rails 2 vs. Rails 3
  • Ruby
  • Run irb in the terminal OR Create a text file (.rb) and run: ruby filename.rb
  • puts(“Hello, World!”)
  • #puts(“Hello, World!”)
  • s = “Hello, World!” puts(s)
  • def say_hello(name) puts(“Hello, #{name}!”) end say_hello(“Daniel”)
  • #look Ma, no parentheses! def say_hello name puts “Hello, #{name}!” end say_hello “Daniel”
  • Everything is an Object
  • 1 + 1
  • 1.class 1.methods 1.+(1)
  • class Fixnum alias :+ :plus end one = 1 one.plus(1)
  • Loop It Up
  • ar = [1, 2, 3] ar.length for i in ar puts i end
  • 3.times do puts “Hip, Hip, Hooray!” end
  • ar = [1, 2, 3] ar.length ar.each do |i| puts i end
  • ar = %w(Red Green Blue) puts ar.inspect ar.each {|w| puts w} puts ar[1]
  • h = { “ r” => “Red”, “ g” => “Green”, “ b” => “Blue” } h.keys h.each do |k, v| puts “#{k}: #{v}” end
  • Rails
  • Rails Principles
  • Convention Over Configuration
  • Don’t Repeat Yourself
  • Model View Controller
  • rails hello_rails
  • Controller
  • script/generate script/generate controller script/generate controller hello
  • app/controllers/hello_controller.rb def index render :text => “Hello!” end
  • Routes
  • config/routes.rb map.connect ‘:controller/:action/:id`
  • map.connect ‘/hello’, :controller => ‘hello’, :action => ‘index’
  • map. hello ‘/hello’, :controller => ‘hello’, :action => ‘index’
  • script/server http://localhost:3000/hello
  • map.connect ‘/hello/ :name ’, :controller => ‘hello’, :action => ‘index’
  • def index render :text => “Hello, #{params[:name]}!” end
  • script/generate controller guess
  • map.connect ‘/guess/:name’, :controller => ‘guess’ --------------------------------- def index if params[:name] == “daniel” render :text => “You got it!” else render :text => “Not quite!” end end
  • map.connect ‘/guess/daniel’, :controller => ‘guess’, :action => ‘right’ map.connect ‘/guess/:name’, :controller => ‘guess’, :action => ‘wrong’
  • app/controllers/guess_controller.rb def right render :text => “You got it!” end def wrong render :text => “Not quite!” end
  • Filters
  • app/controllers/hello_controller.rb before_filter :check_id def index end protected def check_id if params[:id] != “Daniel” redirect_to “http://www.nyu.edu” end end
  • View
  • app/views/guess/right.html.erb --- <h1>Right!</h1> app/views/guess/wrong.html.erb --- <h1>It’s not <%= params[:name] %></h1>
  • Layouts
  • app/views/layouts/main.html.erb
  • <html> <head> <title>Title Goes Here</title> </head> <body> <div id=“header”>…</div> <div id=“main”> <%= yield %> </div> <div id=“footer”>…</div> </body> </html>
  • app/controllers/guess_controller.rb class GuestController < Applica... layout “main” def right end def wrong end end
  • Model
  • ORM
  • mysql> select * from itp_students; +--------+--------+----------------+ | name | email | advisor | +--------+--------+----------------+ | Daniel | dmt321 | Shawn | | Bob | brb555 | DanO | +--------+--------+----------------+
  • student = ItpStudent.new student.name = “Daniel” student.email = “...” student.advisor = “Shawn” student.save
  • INSERT INTO itp_students (name, email, advisor) VALUES (‘Daniel’, ‘...’, ‘Shawn’);
  • student = ItpStudent.find_by_name “Daniel” student.name = “Daniel T” student.save
  • UPDATE itp_students SET name = ‘Daniel T’ WHERE name = ‘Daniel’;
  • config/database.yml
  • script/generate model ItpStudent name:string email:string advisor:string
  • db/migrate/..._create_itp_students.rb
  • script/generate migration add_gpa_to_itp_student gpa:integer
  • rake db:migrate
  • $ script/console Loading development environment (Rails 2.3.5) >> ItpStudent => ItpStudent(id: integer, name: string, email: string, advisor: string, created _at: datetime, updated_at: datetime, gpa: integer)
  • >> student = ItpStudent.new >> student.name = “Daniel” >> student.save!
  • Validations
  • class ItpStudent < ActiveRecord::Base validates_presence_of :name end
  • >> student = ItpStudent.new >> student.valid? => false >> student.save => false >> student.save! ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
  • Scopes
  • class ItpStudent < ActiveRecord::Base named_scope :passing => { :conditions => “gpa > 2” } named_scope :advised_by => {|x| :conditions => {:advisor => x} } end
  • >> ItpStudent.passing >> ItpStudent.advised_by ‘Shawn’ >> ItpStudent.advised_by(‘Shawn’).passing
  • Relations (has_many, belongs_to)
  • class ItpStudent < ActiveRecord::Base has_many :classes belongs_to :itp_class end
  • >> student = ItpStudent.first >> student.classes.first
  • MOAR !!!
  • If you got this far…
    • Partials
    • Helpers (built-in, user-defined)
    • Testing
    • External libraries (plugins, gems)
  • Further Reading
    • ruby-doc.org
    • www.rubyonrails.org
    • guides.rubyonrails.org
    • api.rubyonrails.org
    • planetrubyonrails.com
    • railscasts.com
    • peepcode.com
  • Thanks!