Slide 1

Slide 1 text

Responders

Slide 2

Slide 2 text

©2015 Infinum student academy PostsController#create @post = Post.new(post_params) if @post.save(post_params) redirect_to @post flash[:notice] = 'Post was successfully updated.' else render :new end

Slide 3

Slide 3 text

©2015 Infinum student academy What about internationalization?

Slide 4

Slide 4 text

©2015 Infinum student academy PostsController#create @post = Post.new(post_params) if @post.save flash[:notice] = I18n.t(‘flash.posts.create.notice’) redirect_to @post else render :new end

Slide 5

Slide 5 text

©2015 Infinum student academy The update action is very similar

Slide 6

Slide 6 text

©2015 Infinum student academy PostsController#update @post = Post.find(params[:id]) if @post.update(post_params) flash[:notice] = I18n.t(‘flash.posts.update.notice’) redirect_to @post else render :edit end

Slide 7

Slide 7 text

©2015 Infinum student academy The restful controller pseudocode: initialize_resource can it be saved? flash redirect else render end

Slide 8

Slide 8 text

Meet the responders..

Slide 9

Slide 9 text

©2015 Infinum student academy Convenient wrappers for REST controllers @post = Post.create(post_params) respond_with @post

Slide 10

Slide 10 text

©2015 Infinum student academy Knows what translation to look at

Slide 11

Slide 11 text

©2015 Infinum student academy Universal translation for all controllers flash: actions: create: notice: "%{resource_name} was successfully created." update: notice: "%{resource_name} was successfully updated." destroy: notice: "%{resource_name} was successfully destroyed." alert: "%{resource_name} could not be destroyed."

Slide 12

Slide 12 text

©2015 Infinum student academy Translation per controller flash: posts: create: notice: "Your post was created and will be published soon"

Slide 13

Slide 13 text

©2015 Infinum student academy Need a non-standard redirect location? @post = Post.create(post_params) respond_with @post, location: -> { posts_path }

Slide 14

Slide 14 text

©2015 Infinum student academy Need a non-standard rendering template? @post = Post.create(post_params) respond_with @post, action: ‘shared/posts/new’

Slide 15

Slide 15 text

History

Slide 16

Slide 16 text

©2015 Infinum student academy Responders • Extracted from Rails 4.2 to a separate gem • Maintained by Plataformatec • authors of Devise & Simple Form

Slide 17

Slide 17 text

©2015 Infinum student academy Why were they extracted from Rails?

Slide 18

Slide 18 text

©2015 Infinum student academy respond_to do |format| format.xml { render xml: @model } format.json { render json: @model } end is clearer than… respond_with @model

Slide 19

Slide 19 text

©2015 Infinum student academy I only use them for Html API’s

Slide 20

Slide 20 text

Fin.