As rails is an opinionated framework, it uses the MVC pattern. Whenever it receives a request, it will go through a controller and the controller will render a page to be returned.
def index render :action => :index end
However, sometimes we have to run a longer task.
def index # some time consuming task render :action => :index end
When we met with this situation, this browser will be in the loading-forever mode, and the server might even timeout the connection. One of the way to solve it is to render a view for the user first, telling them “your request is running at the background”, then we continue on our long process. So this is how it’s done:
def index Thread.new do # some time consuming task end render :action => :index end
This way, the web app will spawn another thread for the long task, so the view will be returned to the user first, and the long task will still be executed at the background.