Rails - Intercept respond_with

212 views Asked by At

I am using this 3rd party controller:

   class LibController

      def update
        # 29 lines of code
        respond_with resource
      end

   end

I want to do something other than the respond_with at the end. But I don't want to just copy/paste all 29 lines into MyController.update. Unfortunately I can't figure out a way to render or redirect anywhere else:

   class MyController < LibController

     def update
       super
       redirect_to somewhere_else
     end

   end

I get a DoubleRenderError: Render and/or redirect were called multiple times in this action. I assume this is because respond_with calls render immediately. Is there a way to block/prevent that?

Thanks!

1

There are 1 answers

2
aldrien.h On

I think you are doing a twice redirection. Try to remove one redirection on your update method.

Check sample code below that shows equivalent response when using respond_with.

def create
  @user = User.new(params[:user])
  flash[:notice] = 'User was successfully created.' if @user.save
  respond_with(@user)
end

Which is exactly the same as:

def create
  @user = User.new(params[:user])

  respond_to do |format|
    if @user.save
      flash[:notice] = 'User was successfully created.'
      format.html { redirect_to(@user) }
      format.xml { render xml: @user, status: :created, location: @user }
    else
      format.html { render action: "new" }
      format.xml { render xml: @user.errors, status: :unprocessable_entity }
    end
  end
end