riding rails 4

44
RIDING RAILS 4

Post on 12-Sep-2014

461 views

Category:

Technology


2 download

DESCRIPTION

An overview of some of the new features in Rails 4. All features are discussed in depth in the e-book Upgrade to Rails 4 (http://www.leanpub.com/upgradetorails4) as well.

TRANSCRIPT

Page 1: Riding Rails 4

RIDING RAILS 4

Page 2: Riding Rails 4

#BCNONRAILS4

Page 3: Riding Rails 4

PHILIP DE SMEDT

@PHILIPDESMEDT

PHILIPDESMEDT.COM

Page 4: Riding Rails 4

FULL-STACK DEVELOPER

FREELANCE

(HIRE ME)

Page 5: Riding Rails 4

I WROTE A BOOKUPGRADE TO RAILS 4

Page 6: Riding Rails 4

WHO HAS TRIED RAILS

4?gem install rails --version 4.0.0.beta1 --no-ri --no-rdoc

Page 7: Riding Rails 4

TURBOLINKS

Page 8: Riding Rails 4

LISTENS TO CLICK EVENTS

Page 9: Riding Rails 4

LISTENS TO CLICK EVENTS

LOADS CONTENT

WITHOUT RELOADING THE PAGE

Page 10: Riding Rails 4

LISTENS TO CLICK EVENTSLOADS CONTENT WITHOUT RELOADING THE PAGE

REPLACES BODY+TITLE

USING HTML5 pushState

Page 11: Riding Rails 4

DEMO

Page 12: Riding Rails 4

RUSSIAN DOLL CACHING

Page 13: Riding Rails 4

class Post < ActiveRecord::Base has_many :categoriesend

class Category < ActiveRecord::Base belongs_to :post, touch: trueend

LET’S CACHE

Page 14: Riding Rails 4

<!-- app/views/posts/show.html.erb --><% cache [‘v1’, @post] do %> <h1><%= @post.title %> categories:</h1> <ul><%= render @post.categories %></ul><% end %>

RAILS 3

Page 15: Riding Rails 4

RAILS 3

<!-- app/views/categories/_category.html.erb --><% cache [‘v1’, category] do %> <li> <%= category.title %> <%= link_to “edit”, category %> </li><% end %>

Page 16: Riding Rails 4

views/v1/posts/1-20130228151844

RAILS 3 CACHE KEYS

Page 17: Riding Rails 4

RAILS 3

<!-- app/views/categories/_category.html.erb -->

<% cache [‘v2’, category] do %> <li> <%= category.title %>

<%= link_to “RENAME”, category %> </li><% end %>

Page 18: Riding Rails 4

<!-- app/views/posts/show.html.erb -->

<% cache [‘v2’, @post] do %> <h1><%= @post.title %> categories:</h1> <ul><%= render @post.categories %></ul><% end %>

RAILS 3

Page 19: Riding Rails 4
Page 20: Riding Rails 4

<!-- app/views/categories/_category.html.erb --><% cache category do %> <li> <%= category.title %> <%= link_to “edit”, category %> </li><% end %>

<!-- app/views/posts/show.html.erb --><% cache @post do %> <h1><%= @post.title %> categories:</h1> <ul><%= render @post.categories %></ul><% end %>

Page 21: Riding Rails 4

views/posts/1-20130228151844/fbda9857614e68ed1df453559a761d47

RAILS 4 CACHE DIGEST

Page 22: Riding Rails 4

STRONG PARAMETERS

Page 23: Riding Rails 4

class User < ActiveRecord::Base attr_accessible :name, :emailend def create

@user = User.new(params[:user])@user.save

end

RAILS 3

Page 24: Riding Rails 4

EXAMPLE

Page 25: Riding Rails 4

class CarsController < ApplicationControllerdef create

@car = Car.new(car_params) if @car.save

redirect_to @car else render ‘new’

end endend

RAILS 4

Page 26: Riding Rails 4

private def car_params params.require(:car).permit(:name, :year, :brand) end

require(key) - Ensures that a parameter is present. If it’s present,

returns the parameter at the given key, otherwise raises an

ActionController:: ParameterMissing error.

permit(filters) - Returns a new ActionController::Parameters

instance that includes only the given filters

and sets the permitted attribute for the object to true. This is useful

for limiting which attributes should be allowed for mass updating.

Page 27: Riding Rails 4

LIVE STREAMING

Page 28: Riding Rails 4

ACTIONCONTROLLER::LIVE

MIXIN I/O OBJECTSTREAM DATA TO CLIENT

Page 29: Riding Rails 4

DON’T USE WEBrick

USE PUMA OR RAINBOWS!

Page 30: Riding Rails 4

RUNS IN SEPARATE THREAD

Page 31: Riding Rails 4

CLOSE YOUR STREAM

WHEN DONE

Page 32: Riding Rails 4

PUMA vs. RAINBOWS! vs. NODE.JS

RUBYSNIPPETS.COM

Page 33: Riding Rails 4
Page 34: Riding Rails 4

DEMO

Page 35: Riding Rails 4

ACTIVEMODEL::MODEL

Page 36: Riding Rails 4

FORM WITHOUT ACTIVE

RECORD. WTF?

Page 37: Riding Rails 4

FORM_TAG HELPER

CREATE MODEL

Page 38: Riding Rails 4

EXAMPLE

Page 39: Riding Rails 4

class Contactinclude ActiveModel::Model

attr_accessor :name, :email, :messagevalidates :name, presence: truevalidates :email, presence: truevalidates :message, presence: true,

length: { maximum: 300 }end

contact = Contact.new(name: 'John Doe', email: '[email protected]', message: 'a test')

Page 40: Riding Rails 4

<h1>Contact Us</h1>

<%= form_for @contact do |f| %><%= f.label :name %><%= f.text_field :name %><%= f.label :email %><%= f.email_field :email %><%= f.label :message %><%= f.text_area :message %><%= f.submit 'Submit' %>

<% end %>

Page 41: Riding Rails 4

class ContactsController < ApplicationController

def new

@contact = Contact.new

end

def create

@contact = Contact.new(params[:contact])

if @contact.valid?

UserMailer.new_contact(@contact).deliver

redirect_to root_path, notice: "Message sent! Thanks.”

else

render :new

end

end

end

Page 42: Riding Rails 4

LOTS OF OTHER STUFF

CONTROLLER-WIDE E-TAGS

THREAD SAFETY

ENCRYPTED COOKIES

ROUTING CONCERNS

HTTP PATCH VERB

CUSTOM FLASH TYPESHTML5 FORM HELPERS

SCHEMA CACHE DUMP

NO MORE OBSERVERS/SWEEPERS

INDEX PAGE CONTROLLERACTIVE RESOURCE

PAGE/ACTION CACHING

Page 43: Riding Rails 4

EDUCATION IS BROKEN.I’LL BE TEACHING RAILS…

I <3 DEVBOOTCAMP/STARTER LEAGUE/…

IN BARCELONA.

Page 44: Riding Rails 4

PHILIP DE SMEDT

@PHILIPDESMEDT

PHILIPDESMEDT.COM

COME SAY HI

LEANPUB.COM/UPGRADETORAILS4