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

Riding Rails 4

Riding Rails 4

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.

Philip De Smedt

April 18, 2013
Tweet

Other Decks in Technology

Transcript

  1. LISTENS TO CLICK EVENTS LOADS CONTENT WITHOUT RELOADING THE PAGE

    REPLACES BODY+TITLE USING HTML5 pushState
  2. class  Post  <  Ac+veRecord::Base      has_many  :categories   end

        class  Category  <  Ac+veRecord::Base      belongs_to  :post,  touch:  true   end   LET’S CACHE  
  3. <!-- app/views/posts/show.html.erb --> <% cache [‘v1’, @post] do %> <h1><%=

    @post.title %> categories:</h1> <ul><%= render @post.categories %></ul> <% end %> RAILS 3  
  4. RAILS 3   <!-- app/views/categories/_category.html.erb --> <% cache [‘v1’, category]

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

    category] do %> <li> <%= category.title %> <%= link_to “ RENAME”, category %> </li> <% end %>
  6. <!-- app/views/posts/show.html.erb --> <% cache [‘ v2’, @post] do %>

    <h1><%= @post.title %> categories:</h1> <ul><%= render @post.categories %></ul> <% end %> RAILS 3  
  7. <!-- 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 %>
  8. class User < ActiveRecord::Base attr_accessible :name, :email end def create

    @user = User.new(params[:user]) @user.save end RAILS 3  
  9. class CarsController < ApplicationController def create @car = Car.new(car_params) if

    @car.save redirect_to @car else render ‘new’ end end end RAILS 4  
  10. 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.
  11. class Contact include ActiveModel::Model attr_accessor :name, :email, :message validates :name,

    presence: true validates :email, presence: true validates :message, presence: true, length: { maximum: 300 } end contact = Contact.new(name: 'John Doe', email: '[email protected]', message: 'a test')
  12. <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 %>
  13. 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
  14. LOTS OF OTHER STUFF CONTROLLER-WIDE E-TAGS   THREAD SAFETY  

    ENCRYPTED COOKIES   ROUTING CONCERNS   HTTP PATCH VERB   CUSTOM FLASH TYPES   HTML5 FORM HELPERS   SCHEMA CACHE DUMP   NO MORE OBSERVERS/SWEEPERS   INDEX PAGE CONTROLLER   ACTIVE RESOURCE   PAGE/ACTION CACHING