ViewVariantResponder

Installation

In your Gemfile, add this line:

gem "view_variant_responder"

Configuration

This is only a module, to use it, you have to create your own responder:

# lib/app_responder.rb
class AppResponder < ActionController::Responder
  include Responders::ViewVariantResponder
end

If you’re using any other responders, like Responders::FlashResponder, then include these there too.

Finally you need to configure your application to use it:

require "app_responder"
class ApplicationController < ActionController::Base
  self.responder = AppResponder
end

Usage

Let’s assume you want to show an overlay view of a post. Your controller already should look something like this:

class PostsController < ApplicationController
  respond_to :html

  def show
    @post = Post.find(params[:id])
    respond_with(@post)
  end
end

Now you only need to add a separate view for the overlay

  • app/views/posts/show.html.erb - Your usual view

  • app/views/posts/show.overlay.html.haml - Your overlay view

To get this view using jquery for example, you’ll do something like this:

$.ajax({
  url: "/posts/5",
  dataType: "html",
  headers: {
    "X-View-Variant": "overlay"
  },
  success: function(content) {
    // Voila, your overlay content is now here
  }
});