riding rails 4
Post on 12-Sep-2014
461 views
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
RIDING RAILS 4
#BCNONRAILS4
PHILIP DE SMEDT
@PHILIPDESMEDT
PHILIPDESMEDT.COM
FULL-STACK DEVELOPER
FREELANCE
(HIRE ME)
I WROTE A BOOKUPGRADE TO RAILS 4
WHO HAS TRIED RAILS
4?gem install rails --version 4.0.0.beta1 --no-ri --no-rdoc
TURBOLINKS
LISTENS TO CLICK EVENTS
LISTENS TO CLICK EVENTS
LOADS CONTENT
WITHOUT RELOADING THE PAGE
LISTENS TO CLICK EVENTSLOADS CONTENT WITHOUT RELOADING THE PAGE
REPLACES BODY+TITLE
USING HTML5 pushState
DEMO
RUSSIAN DOLL CACHING
class Post < ActiveRecord::Base has_many :categoriesend
class Category < ActiveRecord::Base belongs_to :post, touch: trueend
LET’S CACHE
<!-- app/views/posts/show.html.erb --><% cache [‘v1’, @post] do %> <h1><%= @post.title %> categories:</h1> <ul><%= render @post.categories %></ul><% end %>
RAILS 3
RAILS 3
<!-- app/views/categories/_category.html.erb --><% cache [‘v1’, category] do %> <li> <%= category.title %> <%= link_to “edit”, category %> </li><% end %>
views/v1/posts/1-20130228151844
RAILS 3 CACHE KEYS
RAILS 3
<!-- app/views/categories/_category.html.erb -->
<% cache [‘v2’, category] do %> <li> <%= category.title %>
<%= link_to “RENAME”, category %> </li><% end %>
<!-- app/views/posts/show.html.erb -->
<% cache [‘v2’, @post] do %> <h1><%= @post.title %> categories:</h1> <ul><%= render @post.categories %></ul><% end %>
RAILS 3
<!-- 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 %>
views/posts/1-20130228151844/fbda9857614e68ed1df453559a761d47
RAILS 4 CACHE DIGEST
STRONG PARAMETERS
class User < ActiveRecord::Base attr_accessible :name, :emailend def create
@user = User.new(params[:user])@user.save
end
RAILS 3
EXAMPLE
class CarsController < ApplicationControllerdef create
@car = Car.new(car_params) if @car.save
redirect_to @car else render ‘new’
end endend
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.
LIVE STREAMING
ACTIONCONTROLLER::LIVE
MIXIN I/O OBJECTSTREAM DATA TO CLIENT
DON’T USE WEBrick
USE PUMA OR RAINBOWS!
RUNS IN SEPARATE THREAD
CLOSE YOUR STREAM
WHEN DONE
PUMA vs. RAINBOWS! vs. NODE.JS
RUBYSNIPPETS.COM
DEMO
ACTIVEMODEL::MODEL
FORM WITHOUT ACTIVE
RECORD. WTF?
FORM_TAG HELPER
CREATE MODEL
EXAMPLE
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')
<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 %>
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
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
EDUCATION IS BROKEN.I’LL BE TEACHING RAILS…
I <3 DEVBOOTCAMP/STARTER LEAGUE/…
IN BARCELONA.
PHILIP DE SMEDT
@PHILIPDESMEDT
PHILIPDESMEDT.COM
COME SAY HI
LEANPUB.COM/UPGRADETORAILS4