Async rendering Rails page



Example:
We have movie with rating, which gets from IMDB
# app/controllers/movies_controller.rb
class MoviesController < ApplicationController
def show
@movie = Movie.find(params[:id])
@movie_rating = IMDB.movie_rating(@movie)
end
end
# app/views/movies/show.html.haml
%h1
Information about #{@movie.title}
%p= @movie.description
%p
Movie rating on IMDB: #{@movie_rating}
view raw async-01.rb hosted with ❤ by GitHub


But page will load only after rating got from IMDB site.
To not wait for it - we use async

# app/views/movies/show.html.erb
%h1
Information about #{@movie.title}
%p= @movie.description
= render_async movie_rating_path(@movie.id)
# config/routes.rb
get :movie_rating, controller: :movies
# app/controllers/movies_controller.rb
def movie_rating
@movie_rating = IMDB.movie_ratings(@movie)
render partial: 'movie_ratings'
end
# app/views/movies/_movie_rating.html.erb
%p
Movie rating on IMDB: #{@movie_rating}
# app/views/layouts/application.html.erb
= content_for :render_async
view raw async-02.rb hosted with ❤ by GitHub

https://semaphoreci.com/

Comments