Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Intro to Rails | UCSB ACM

Andrew Berls
February 28, 2013

Intro to Rails | UCSB ACM

Introduction to web application development with Ruby on Rails

Andrew Berls

February 28, 2013
Tweet

More Decks by Andrew Berls

Other Decks in Programming

Transcript

  1. What is Rails? • Open-source web framework "optimized for programmer

    happiness" * • Lets you quickly build and deploy dynamic web applications • Clearly defined conventions and philosophy lead to powerful and expressive code * This is a direct quote.
  2. Ruby • A dynamic, interpreted language with a focus on

    simplicity and productivity • Combines aspects of object-oriented, functional, and imperative programming • Version 2.0.0 was just released on Sunday
  3. Rails 'Philosophy' • Convention over configuration ◦ All Rails projects

    have the same directory structure ◦ User model -> users table, user.rb, etc • Don't repeat yourself (DRY) ◦ Information in a single, unambiguous place ◦ Ex: layouts and partials
  4. Full Stack Development • Rails works with all layers of

    a web application • Request routing • Database querying / schema management • Template rendering • etc...
  5. Learn by Example • The classic Rails app: A blog

    • A trivial CRUD app - create, read, update, delete • How would we structure this app the Rails way?
  6. Models • Represents your application domain objects ◦ Typically the

    nouns in an application ◦ A blog might have posts and users • Usually a 1-to-1 correspondence to a database table ◦ Ex: Post model implicitly tied to posts table • Home of validation and business logic ◦ "Fat model, skinny controller"
  7. • Rails lets you declare a variety of associations between

    your models using a natural syntax • :post belongs_to :user • :customer has_many :orders • :supplier has_one :account Model Associations
  8. • :post belongs_to :user ◦ Must also specify inverse: user

    has_many posts • Now you can write: user.posts # => [<Post 1>, <Post 2>] post.user # => <User 1> A one-to-many association
  9. • Want to add tags to our blog posts (ex:

    'ruby, programming, rails') • We could store a tag string on every post and then search through all posts for a match when looking for a tag • Insanely inefficient. How else to do it? Many-to-many associations
  10. • post has_and_belongs_to_many tags • tag has_and_belongs_to_many posts • Need

    a join table between post_ids and tag_ids • Point is that after we declare the associations and create the table, Rails will take care of the heavy lifting with the foreign keys A many-to-many association
  11. Views • The user interface of an application - what

    the user sees in their browser and interacts with • Scroll through a list of posts, or read a specific post • Links and forms send requests/data to the controller
  12. Controllers • 'Glue' between models and views • Process requests

    from the browser, query the model for data, and pass that data to the views for presentation
  13. Rails is RESTful • Representational state transfer • An architectural

    style developed by Roy Fielding • In Rails, resources provide automatic mapping between HTTP verbs and URLs to controller actions
  14. MAGIC • Dynamic finders User.find_by_email("[email protected]") • Resource paths <%= link_to

    "New Post", new_post_path %> • Lots of extensions to Ruby core ◦ "Is this method defined in Ruby or Rails???"
  15. Database Adapters • ActiveRecord (Rails ORM) has adapters for working

    with most of the databases out there • Presents a single persistence API for the SQL variants: MySQL, PostgreSQL, Oracle, SQLite, ... • Can also plug in Redis, MongoDB, etc
  16. Sass • CSS preprocessor • Makes CSS more sane with

    variables, nesting, mixins, and more • http://sass-lang.com/
  17. CoffeeScript • A little language that compiles into JavaScript •

    Syntactic sugar enhances brevity/readability, and even adds a few features such as list comprehensions • http://coffeescript.org/
  18. Security • CSRF: One-liner to automatically generate security token for

    forms and compare them against a server-side secret • Mass-assignment attribute whitelisting • XSS: output is automatically escaped Recommended: The Rails Security Guide
  19. Where to go from here • Ruby on Rails Tutorial

    by Michael Hartl • Agile Web Development with Rails • The Official Rails Guides