Slide 1

Slide 1 text

Hidden features of Rails 5

Slide 2

Slide 2 text

Prathamesh Sonpatki @_cha1tanya [email protected]

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Pune, India

Slide 5

Slide 5 text

Pune, India

Slide 6

Slide 6 text

Pune, India

Slide 7

Slide 7 text

Pune, India

Slide 8

Slide 8 text

Pune, India

Slide 9

Slide 9 text

I also got more professional skills

Slide 10

Slide 10 text

PHP

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

I don’t have Stickers

Slide 13

Slide 13 text

Stickers

Slide 14

Slide 14 text

T-Shirts are fine too

Slide 15

Slide 15 text

5

Slide 16

Slide 16 text

5

Slide 17

Slide 17 text

5

Slide 18

Slide 18 text

5

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

New welcome page

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

Rendering views outside of controller

Slide 23

Slide 23 text

Rendering views outside of controller

Slide 24

Slide 24 text

Rendering views outside of controller

Slide 25

Slide 25 text

OrdersController.render :show

Slide 26

Slide 26 text

OrdersController.render :show

Slide 27

Slide 27 text

OrdersController.render json: Order.all

Slide 28

Slide 28 text

OrdersController.render :show

Slide 29

Slide 29 text

OrdersController.render :show, assigns: { order: Order.last } # /app/views/orders/show.html.erb <%= @order.uid %>

Slide 30

Slide 30 text

OrdersController.render :show, assigns: { order: Order.last } # /app/views/orders/show.html.erb <%= @order.uid %>

Slide 31

Slide 31 text

Request environment

Slide 32

Slide 32 text

request.env

Slide 33

Slide 33 text

Request environment

Slide 34

Slide 34 text

Request environment

Slide 35

Slide 35 text

>> OrdersController.renderer.defaults => {:http_host=>"example.org", :https=>false, :method=>"get", :script_name=>"", :input=>""}

Slide 36

Slide 36 text

renderer = OrdersController.renderer.new(https: true) renderer.render template: ‘show'

Slide 37

Slide 37 text

renderer = OrdersController.renderer.new(https: true) renderer.render template: ‘show' # order_url => “https://example.org/orders/1”

Slide 38

Slide 38 text

https://github.com/brainopia/backport_new_renderer

Slide 39

Slide 39 text

Faster rendering of partials from cache

Slide 40

Slide 40 text

# index.html.erb <%= render partial: 'todo', collection: @todos %> # _todo.html.erb <% cache todo do %> <%= todo.name %> <% end %>

Slide 41

Slide 41 text

# index.html.erb <%= render partial: 'todo', collection: @todos %> # _todo.html.erb <% cache todo do %> <%= todo.name %> <% end %>

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

Folded into Rails 5

Slide 44

Slide 44 text

# index.html.erb <%= render partial: 'todo', collection: @todos, cached: true %> # _todo.html.erb <% cache todo do %> <%= todo.name %> <% end %>

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

Per job active job adapter

Slide 47

Slide 47 text

Per job active job adapter

Slide 48

Slide 48 text

config.active_job.queue_adapter = :sidekiq

Slide 49

Slide 49 text

config.active_job.queue_adapter = :sidekiq

Slide 50

Slide 50 text

config.active_job.queue_adapter = :sidekiq

Slide 51

Slide 51 text

class EmailJob < ActiveJob::Base self.queue_adapter = :sidekiq end class NewletterJob < ActiveJob::Base self.queue_adapter = :delayed_job end

Slide 52

Slide 52 text

class EmailJob < ActiveJob::Base self.queue_adapter = :sidekiq end class NewletterJob < ActiveJob::Base self.queue_adapter = :delayed_job end EmailJob.queue_adapter => # NewletterJob.queue_adapter => #

Slide 53

Slide 53 text

Caching in development

Slide 54

Slide 54 text

Caching in development

Slide 55

Slide 55 text

# config/environments/development.rb config.action_controller.perform_caching = false

Slide 56

Slide 56 text

# config/environments/development.rb config.action_controller.perform_caching = true

Slide 57

Slide 57 text

$ rails dev:cache Development mode is now being cached.

Slide 58

Slide 58 text

config.action_controller.perform_caching = true config.static_cache_control = "public, max- age=172800" config.cache_store = :mem_cache_store

Slide 59

Slide 59 text

$ rails dev:cache Development mode is no longer being cached.

Slide 60

Slide 60 text

Puma as default web server

Slide 61

Slide 61 text

Puma as default web server

Slide 62

Slide 62 text

Silent asset logs in development

Slide 63

Slide 63 text

Started GET "/assets/ application.self-4a04ce68c5ebf2d39fba46316802f17d0 a73fadc4d2da50a138d7a4bf2d26a84.css?body=1" for 127.0.0.1 at 2016-09-02 10:23:04 +0530 Started GET "/assets/bootstrap/ transition.self-6ad2488465135ab731a045a8ebbe3ea2fc 501aed286042496eda1664fdd07ba9.js?body=1" for 127.0.0.1 at 2016-09-02 10:23:04 +0530

Slide 64

Slide 64 text

# config/environments/development.rb config.assets.quiet = true

Slide 65

Slide 65 text

Finer control over SSL usage

Slide 66

Slide 66 text

Redirect http to https

Slide 67

Slide 67 text

Redirect http to https

Slide 68

Slide 68 text

config.force_ssl = true config.ssl_options = { redirect: { status: 307, port: 81 } }

Slide 69

Slide 69 text

HSTS headers

Slide 70

Slide 70 text

config.ssl_options = { hsts: { preload: true } }

Slide 71

Slide 71 text

config.ssl_options = { hsts: { expires: 10.days } }

Slide 72

Slide 72 text

Controller actions default to 204 No Content

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

Browser request

Slide 76

Slide 76 text

def create @post = Post.new(post_params) if @post.save redirect_to @post, notice: 'Post was successfully created.’ else render ‘new’ end end

Slide 77

Slide 77 text

Started POST "/posts" for ::1 at 2016-06-02 09:24:53 -0400 Processing by PostsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"FaRp8NUAZWIW0WnMzLpKKQht9WWeTxeSvOvO2GeFpkGbOXzYEnvxTKEZf7gwY 4oy6cW+GRQ3UE6Zu0iXGB4I5A==", "post"=>{"content"=>"hello"}, "commit"=>"Create Post"} (0.2ms) begin transaction SQL (0.3ms) INSERT INTO "posts" ("content", "created_at", "updated_at") VALUES (?, ?, ?) [["content", "hello"], ["created_at", 2016-06-02 13:24:53 UTC], ["updated_at", 2016-06-02 13:24:53 UTC]] (0.7ms) commit transaction Redirected to http://localhost:3000/posts/1 Completed 302 Found in 6ms (ActiveRecord: 1.3ms)

Slide 78

Slide 78 text

def create @post = Post.new(post_params) if @post.save else render ‘new’ end end

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

Started POST "/posts" for ::1 at 2016-06-02 09:25:11 -0400 Processing by PostsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"RC46YER7l5yQz1J+DkZIWINBixkaMz9qWWlFzvZT8K3Ksy9IgwADsicHRAryn 4hDYunAZZBLeLZ8OcOBicheCA==", "post"=>{"content"=>"hello 2"}, "commit"=>"Create Post"} (0.0ms) begin transaction SQL (0.2ms) INSERT INTO "posts" ("content", "created_at", "updated_at") VALUES (?, ?, ?) [["content", "hello 2"], ["created_at", 2016-06-02 13:25:11 UTC], ["updated_at", 2016-06-02 13:25:11 UTC]] (1.5ms) commit transaction No template found for PostsController#create, rendering head :no_content Completed 204 No Content in 64ms (ActiveRecord: 2.2ms)

Slide 81

Slide 81 text

Started POST "/posts" for ::1 at 2016-06-02 09:25:11 -0400 Processing by PostsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"RC46YER7l5yQz1J+DkZIWINBixkaMz9qWWlFzvZT8K3Ksy9IgwADsicHRAryn 4hDYunAZZBLeLZ8OcOBicheCA==", "post"=>{"content"=>"hello 2"}, "commit"=>"Create Post"} (0.0ms) begin transaction SQL (0.2ms) INSERT INTO "posts" ("content", "created_at", "updated_at") VALUES (?, ?, ?) [["content", "hello 2"], ["created_at", 2016-06-02 13:25:11 UTC], ["updated_at", 2016-06-02 13:25:11 UTC]] (1.5ms) commit transaction No template found for PostsController#create, rendering head :no_content Completed 204 No Content in 64ms (ActiveRecord: 2.2ms)

Slide 82

Slide 82 text

Don’t forget to respond

Slide 83

Slide 83 text

Don’t forget to me

Slide 84

Slide 84 text

ActionController Parameters no longer inherits from hash ⚠

Slide 85

Slide 85 text

Action Controller Parameters decide what to permit and what to not

Slide 86

Slide 86 text

user_params.is_a? Hash #=> true

Slide 87

Slide 87 text

user_params.is_a? ActionController::Parameters #=> true

Slide 88

Slide 88 text

user_params.to_h # Safe hash

Slide 89

Slide 89 text

user_params.to_unsafe_h # Unsafe hash

Slide 90

Slide 90 text

>> user_params.inject DEPRECATION WARNING: Method inject is deprecated and will be removed in Rails 5.1, as `ActionController::Parameters` no longer inherits from hash. Using this deprecated behavior exposes potential security problems. If you continue to use this method you may be creating a security vulnerability in your app that can be exploited. Instead, consider using one of these documented methods which are not deprecated: http://api.rubyonrails.org/v5.0.0/classes/ActionController/ Parameters.html

Slide 91

Slide 91 text

Fragment Caching Action Mailer views

Slide 92

Slide 92 text

Fragment Caching Action Mailer views

Slide 93

Slide 93 text

config.action_mailer.perform_caching = true

Slide 94

Slide 94 text

<% cache 'signup-text' do %>

Welcome!

You have successfully signed up. Your username is:
<% end %> <%= @user.login %>.

<%= render :partial => 'footer' %>

Slide 95

Slide 95 text

<% cache 'signup-text' do %>

Welcome!

You have successfully signed up. Your username is:
<% end %> <%= @user.login %>.

<%= render :partial => 'footer' %>

Slide 96

Slide 96 text

Cache digest for app/views/user_mailer/_footer.erb: 7313427d26cc1f701b1e0212498cee38 Cache digest for app/views/user_mailer/welcome_email.html.erb: 30efff0173fd5f29a88ffe79a9eab617 Rendered user_mailer/_footer.erb (0.3ms) Rendered user_mailer/welcome_email.html.erb (26.1ms) Cache digest for app/views/user_mailer/welcome_email.text.erb: 77f41fe6159c5736ab2026a44bc8de55 Rendered user_mailer/welcome_email.text.erb (0.2ms) UserMailer#welcome_email: processed outbound mail in 190.3ms

Slide 97

Slide 97 text

Want to know more about Rails 5?

Slide 98

Slide 98 text

Rails 5 Blog Series blog.bigbinary.com /categories/Rails-5

Slide 99

Slide 99 text

Rails 5 Blog Series blog.bigbinary.com /categories/Rails-5

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

This Week in Rails rails-weekly. ongoodbits.com

Slide 102

Slide 102 text

This Week in Rails rails-weekly. ongoodbits.com

Slide 103

Slide 103 text

Rails 5.1.0 beta1 is out!

Slide 104

Slide 104 text

Fin @_cha1tanya @BigBinary [email protected]