Rails review
Upcoming SlideShare
Loading in...5

Rails review



ATLRUG Emerald City Programming Group, April 23, 2011.

ATLRUG Emerald City Programming Group, April 23, 2011.



Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



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.

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

Rails review Rails review Presentation Transcript

  • Rails Review Alan Hecht http://alanhecht.me
  • Building a Rails Application
        • How would you build emerald_blogger
        • Rails 3 application architecture
  • Application Setup
        • rails new emerald_blogger
        • Edit Gemfile
          • bundle install
        • Add project to Git
        • Set up project in GitHub
        • Link project to GitHub
  • App. Configuration Client Files
        • Located in the ‘public’ directory
        • HTML, image, JavaScript, and stylesheet files
        • Default page public/index.html
  • App. Configuration - Environments
        • Three environments: development, test, & production
        • Can add additional environments (i.e. acceptance)
  • App. Configuration - Database
        • config/database.yml has database configuration
        • Databases located in db/
        • db/schema.rb contains the database schema after migrations are applied
        • db/seeds.rb contains database seed data which is run once
  • App. Configuration - Defaults
        • Located in config/application.rb
        • New to Rails 3
  • Application Code
        • Located in app/
          • app/models
          • app/views
          • app/controllers
          • app/helpers
            • Helper code for models, views, and controllers
            • Primarily seen it used for view helpers, with some controller helpers
  • Post Setup - Model
        • rails generate model Post title:string, content:text, post_date:date
        • Model, migration, and test were created
  • Post Setup - Validation
        • In Rails 3, validations are “validates <field(s)>, <condition>”
        • validates :title, :content, :presence => true
  • Post Setup - Migration
        • rails generate migration AddLikesToPosts num_likes:integer
        • run with “rake db:migrate”
        • Can add an index to a table as a migration
  • Post Setup - Routing
        • config/routes.rb
        • A “resource route” maps HTTP verbs to CRUD actions
        • Add a “resources :posts” line to file
        • rake routes – lists the routes and their actions
        • Must restart the application after changing the file
  • Post Setup - Controller
        • Ties the view and the model together
        • Sets up the model(s) for use by the view
        • The view with the same action will be rendered unless a different view is specified
          • i.e. render :edit
          • Controller code for different view is not executed
        • Can specify what view layout to use along with alternatives to HTML like XML or JavaScript
  • Views - Layouts
        • Page template
        • View code for an action executed by the “yield” statement
          • - Implemented using a Ruby block
  • Post Setup - View
        • app/views/posts directory
  • Views - Partials
        • app/views/posts/_form.html.erb
          • Underscore in file name signifies that it’s a partial
        • Represents a portion of the view on a specific page that serves a specific purpose or is reused.
        • Can pass parameters into partials
        • Most of the time, the partial is loaded in the view code
          • Can render a partial directly from the controller
  • App. Configuration Default Home Page
        • Remove ‘public/index.html’
        • Add the line ‘root :to => “<controller>#<action” in config/routes.rb
        • Restart the application
  • Comments Setup Active Record Associations
        • Must be associated in the model and the database
        • For a “one-to-many” relationship:
          • Add the foreign key to the parent in the child table
          • Add a “ has_many <children> ” line to the parent model
  • Deploy to Heroku
        • Edit Gemfile
          • Add “gem heroku” line
        • bundle install
        • heroku create
        • git push –u heroku master
        • heroku open