Rolle
Rolle

Reputation: 13

Rendering text before and after an action works

I have an application with a sometimes longer executing action. How might I, if possible, render placeholder text such as 'processing...' before the action executes and replace the page with the result of that action, given that you can only issue a render once per action?

Upvotes: 1

Views: 133

Answers (1)

Vlad Khomich
Vlad Khomich

Reputation: 5880

the :text option of the render method accepts a Proc object:

render :text => proc { |response, output|
   5.times do |i|
     output.write("Hello, friend\n")
     sleep 3
   end
}

Your example might look like :

 render :text => proc {|response, output|
    output.wirte("Processing..")
    results = perform_something
    output.write(results) 
 }

As you see, the 'output' here is an writable IO-like object. But still, you should avoid using it whenever possible. You can show the 'Processing ...' message with javascript easly here, then receive an xhr back from controller once you have the results to display

Upvotes: 1

Related Questions