Plugins - response_for

StarAdd to favorites

response_for allows you to decorate your respond_to actions on sublcassed controllers, so you don’t have to rewrite the entire action.

Example

  class FooController < ApplicationController
    def index
      @foos = Foo.find(:all)
    end

    def show
      @foo = Foo.find(params[:id])
    end
  end

  # this controller needs to respond_to fbml on index, and
  # js, html and xml (templates) on index and show
  class SpecialFooController < FooController
    response_for :index do |format|
      format.fbml { render :inline => turn_into_facebook(@foos) }
    end

    response_for :index, :show, :types => [:html, :xml, :js]
  end

Usage

  response_for :action1 [, :action2], [,:types => [:mime, :type, :list]] [,:replace => ] [ do |format| ... end]

For example:

  response_for :index, :types => [:fbml]    # index will respond to fbml and try to render, say, index.fbml.builder

  response_for :update do |format|          # this example is for a resources_controller controller
    if resource.valid?
      format.js { render(:update) {|page| page.replace(dom_id(resource), :partial => resource}}
    else
      format.js { render(:update) {|page| page.visual_effect :shake, dom_id(resource) }}
    end
  end

  response_for :index, :replace => true do |format|   # will ignore index's current respond_to block, and use the following
    format.xml
    format.js
  end

Notes

  • You don’t need to have a respond_to block in the action for response_for to work
  • You can stack up multiple response_for calls, the most recent has precedence
  • the specifed block is executed within the controller instance, so you can use controller instance methods are instance variables (i.e. you can make it look just like a regular respond_to block)
  • you can add a response_for an action that is just a public template (where there is no actual action method defined)
  • you can combine the :types option with a block, the block has precedence if you specify the same mime type in both.

Ian White

http://blog.ardes.com/articles/tag/response_for

http://svn.ardes.com/rails_plugins/response_for

Rails' (MIT)

  • Currently 4.0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Controllers

Tags

Comments

Add a comment

Search Plugins

Query syntax

Plugins by Category

Sponsors

Rails Kits: Get Code. Get Moving.

Have a comment?