Using responders with a respond_with that takes a block

1.5k views Asked by At

I was running rails 3.2.1 and using rails 3 responders as well as the flash responder available in the responders gem: https://github.com/plataformatec/responders

For some of my controller actions I always want to redirect back to the previous URL but display a flash message if the object is created or not, it looks something like this:

class MyController < ActionController::Base
  responders :flash
  respond_to :html

  def create
    @my_object = MyObject.create(params[:my_object])
    respond_with @my_object do |format|
      format.html { redirect_to :back }
    end
  end
end

This works fine in rails 3.2.1 but seems to have stopped working in 3.2.2, there seems to have been some modifications about how respond_with functions when taking a block. Specifically this patch: https://github.com/rails/rails/pull/4870/files

I was wondering if there's any way to achieve this same behaviour and use the flash responder to set the flash messages (I don't want to have to do it manually).

2

There are 2 answers

0
Mario Visic On BEST ANSWER

Turned out to be a bug in rails 3.2.2 https://github.com/rails/rails/pull/5299

0
shingara On

You need create your own responder to do your redirect all the time and include it on your Controller.

The responder are like Rack::Middleware. You can cumulate it.