1

I have a controller action like this:

  def get_build_output
    @project = Project.find(params[:project_id])
    @build_num = params[:build_num]

    has_more = true
    while has_more == true
      response = jenkins_client.job.get_console_output(@project.name, @build_num, 0, "html")
      @output = response["output"]
      has_more = response["more"]
      respond_to do |format|
        format.js
      end
    end
  end

And I have a get_build_output.js.erb file with:

$("#build_output").append("<%= raw escape_javascript(@output) %>");

What I'm trying to is continuously get output from a remote call and render it into the view, until has_more is false. Currently, the setup above only renders once and no more.

How can I re-render the page multiple times from the controller? And is there a better way to do what I'm trying to accomplish?

Snowman
  • 31,411
  • 46
  • 180
  • 303

1 Answers1

1

You don't need to render the page more than once. Just accumulate all the output in @output until there is none left, then render the page. The code you posted will almost do that -- but first, you need to move the respond_to call outside of the loop, and second, rather than @output =, you need @output <<. Also, you need to initialize @output to an empty string before the loop.

If you want to stream the output to the client as it is available, you can also do that. But that's a different question. If that's what you really want, see this: Ruby on Rails 3: Streaming data through Rails to client

If you want to stream the output to the client, and you need to use your template to generate the output for each iteration of the loop, you can render a template to string with render_to_string. Here is the documentation: http://api.rubyonrails.org/classes/AbstractController/Rendering.html#method-i-render_to_string

Community
  • 1
  • 1
Alex D
  • 29,755
  • 7
  • 80
  • 126