• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Intro to Ruby on Rails
 

Intro to Ruby on Rails

on

  • 363 views

Brief introduction to basics of Ruby on Rails.

Brief introduction to basics of Ruby on Rails.

Statistics

Views

Total Views
363
Views on SlideShare
363
Embed Views
0

Actions

Likes
0
Downloads
2
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

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

    Intro to Ruby on Rails Intro to Ruby on Rails Presentation Transcript

    • Ruby on RailsJune 17, 2011Elpizo Choi
    • What is Ruby on Rails?
      MVC Framework for building web apps in Ruby
      Uses Ruby classes to abstract out the various components of an app
      Convention over configuration
    • Main Components
      ActiveRecord(ORM – think hibernate)
      ActionController(business logic, routing)
      ActionView(template parser)
      Migrations, rake tasks
    • Creating a Rails app
    • Basic folder structure
      apps folder
      controllers, models, views – where you’ll spend most your time
      database.yml
      Configures database
      Gemfile
      Configures external libraries used in app
    • … a little configuration
    • …a little configuration
    • Let’s make a model
    • ActiveRecord::Base
      When inherited, maps to table that’s plural of class name
      Team -> teams
      Basic methods
      Team.create()
      Team.find()
      Team.where().limit().sort().skip().first
      Team.where().limit().sort().skip().all
    • …first we need to create table
    • …first we need to create table
    • …first we need to create table
      Rake
      • Ruby build tool
      • rake –T to see all available tasks
      • rake db:migrate will run all database migrations that haven’t been run
      • Stores migrations that have already been run
    • Create a new Team
    • Querying
      More in-depth tutorial:
      http://guides.rubyonrails.org/active_record_querying.html
    • The “web” part of web-app
    • Let’s get this running
    • What happened?
    • What happened?
    • What happened?
      config/routes.rb defines how urls are mapped to controllers
      get “teams/index” is shorthand for:
      match “teams/index” => “teams#index”, :via => :get
      More details: http://guides.rubyonrails.org/routing.html
      Read comments in config/routes.rb
    • Let’s make some changes
    • Let’s make some changes
    • Let’s make some changes
    • Controller
      • Logic to be run every time the mapped route is hit
      • All code in the corresponding method will happen as side effects
      • Instanced variables set here are available to the template
    • View
      • .erb is E-Ruby
      • Add as extension to allow the default Rails template engine to parse Ruby code inside HTML
    • View
      • Basic HTML
      • <% %> tag: Ruby code to be run, but not shown
      • <%= %>tag: Ruby code to be run and shown
      • ^^ important: because everything in Ruby returns something (in this case it’ll be @team object from the block)
    • Creating relationships
    • Creating relationships
      Create the player
      Add foreign key
    • Creating relationships
    • Creating relationships
      Gotta reload console after changing models
      Get players
    • But wait! There’s a shortcut!
      has_many basically does the following:
      • looks in a table with same name as its argument (players)
      • for records whose foreign key column corresponds to the current class (team_id)
      • and where the value is the current instance id (1)
    • Likewise…
      You can either do this….
      …or this:
      It’ll accomplish the same thing:
    • We can now change our view
    • We can now change our view
    • More associations
      ActiveRecord also has shortcuts for:
      has_and_belongs_to (join tables)
      Polymorphic associations (belongs_to different objects)
      http://guides.rubyonrails.org/association_basics.html
    • Congratulations!
      You’ve made your first Rails app
    • Additional Resources
      Source code for example_app:
      https://github.com/fuJiin/example_app
      See it live:
      http://cg-example-app.heroku.com/teams/index
      Documentation and light tutorials:
      http://guides.rubyonrails.org/
      More in depth:
      http://ruby.railstutorial.org/ruby-on-rails-tutorial-book
    • Questions?