Alex Wayne
Alex Wayne

Reputation: 187034

Rails 2.x get rendered partial as string and then respond with other content

I need to render a partial in a controller, but not as the response to a request. Problem is that if I call render :partial => 'foo' like I do in views, the controller thinks I'm rendering a response. Then later when I render the real intended response (or redirect) I get a wonderful ActionController::DoubleRenderError.

class SalesController < ApplicationController
  def send_content_to_some_api
    @sale = Sale.find params[:id]
    SomeApi.send_content(
      render(:partial => 'some_content',
             :locals => { :sale => @sale })
    )

    flash[:notice] = 'Totally just sent that content!'
    redirect_to sale_path(@sale)
  end
end

I could get around this by putting it in a view, but that seems to break MVC, and makes a redirect messier.

How can I get the result of a partial in a string without the controller trying to use it as a response?

Upvotes: 1

Views: 738

Answers (1)

Xorlev
Xorlev

Reputation: 8643

See ActionController::Base#render_to_string

Upvotes: 2

Related Questions