Top Banner
Beginning Rails Ruby Fools 2008 - Copenhagen
28

Beginning Rails - jaoo.dk

Jan 13, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Beginning Rails - jaoo.dk

Beginning Rails

Ruby Fools 2008 - Copenhagen

Page 3: Beginning Rails - jaoo.dk

My BookBeginning Rails

From Novice to Professional

Page 4: Beginning Rails - jaoo.dk

The Good Web Framework

• Full stack

• Open source

• Cross platform

Page 5: Beginning Rails - jaoo.dk

Ruby on Rails

• Agile

• Less Software

• Convention over Configuration

• Rails is Ruby

• MVC

Page 6: Beginning Rails - jaoo.dk

The Layers of MVC

• Model

• View

• Controller

Page 7: Beginning Rails - jaoo.dk

Model

• Objects represent the database

• Code

Event.find :all

self.update_attribute :title, '@ Ruby Fools'

• ActiveRecord

Page 8: Beginning Rails - jaoo.dk

View

• Renders the visible part of the application

• ActionView

Page 9: Beginning Rails - jaoo.dk

Controller

• Code:

class EventsController << ApplicationController

def index

# logic to list events

end

• ActionController

Page 10: Beginning Rails - jaoo.dk

You will need

• Ruby

• RubyGems

• Rails

• MySQL - or another database

Page 11: Beginning Rails - jaoo.dk

Creating a new app

• creteil:~/code$ rails events

create

create app/controllers

create app/helpers

create app/models

create app/views/layouts

• creteil:~/code$ cd events

• creteil:~/code/events$ ./script/server

Page 12: Beginning Rails - jaoo.dk

Up and Running

Page 13: Beginning Rails - jaoo.dk

Directory structure

Page 14: Beginning Rails - jaoo.dk

Let’s create the events application

Page 15: Beginning Rails - jaoo.dk

creteil:~/code/events$ ./script/generate scaffold

event title:string location:string

occurs_on:date description:text

Page 16: Beginning Rails - jaoo.dk

Migration

• Code:

create_table :events do |t|

t.string :title

t.string :location

t.date :occurs_on

t.text :description

t.timestamps

end

• Ruby-based

Page 17: Beginning Rails - jaoo.dk

Model

• Code:

class Event < ActiveRecord::Base

end

Page 18: Beginning Rails - jaoo.dk

View

Page 19: Beginning Rails - jaoo.dk

Controller

• Code:

def index

@events = Event.find(:all)

respond_to do |format|

format.html # index.html.erb

format.xml { render :xml => @events }

end

end

Page 20: Beginning Rails - jaoo.dk

Improvements

• Add validationvalidates_presence_of :title, :location

• Extend functionality

Page 21: Beginning Rails - jaoo.dk

Plugins

• Mechanism to easily extend Rails

• Some examplesrestful_authenticationacts_as_taggable_on_steroidshaml...

• http://agilewebdevelopment.com/plugins

Page 22: Beginning Rails - jaoo.dk

Custom layout

Page 23: Beginning Rails - jaoo.dk

Ajax on Rails

Page 24: Beginning Rails - jaoo.dk

RoR makes Ajax easy

• Prototype + script.aculo.us

• link_to becomes link_to_remote

• form_for becomes remote_form_for

• Plugins for auto-complete, drag & drop, sortable list

Page 25: Beginning Rails - jaoo.dk

RJS templates

• Generates Javascript code from Ruby

page.hide dom_id(@event)

becomes

try {

Element.hide("event_16");

} catch (e) { .. }

Page 26: Beginning Rails - jaoo.dk

Things that help

• Freeze Railsrake rails:freeze:edgerake rails:freeze:gems

• API at fingertipsrake docs:rails

Page 27: Beginning Rails - jaoo.dk

Summary

• Rails is simple

• Rails is powerful

• Rails is comprehensive

• Have fun

Page 28: Beginning Rails - jaoo.dk

Thank you