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

Responders

DamirSvrtan
September 14, 2015

 Responders

Dry-out your controllers with some responders

DamirSvrtan

September 14, 2015
Tweet

More Decks by DamirSvrtan

Other Decks in Programming

Transcript

  1. ©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
  2. ©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
  3. ©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
  4. ©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."
  5. ©2015 Infinum student academy Translation per controller flash: posts: create:

    notice: "Your post was created and will be published soon"
  6. ©2015 Infinum student academy Need a non-standard redirect location? @post

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

    = Post.create(post_params) respond_with @post, action: ‘shared/posts/new’
  8. ©2015 Infinum student academy Responders • Extracted from Rails 4.2

    to a separate gem • Maintained by Plataformatec • authors of Devise & Simple Form
  9. ©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