Module: ActionController::Components

Defined in:
lib/action_controller/components.rb

Overview

Components allow you to call other actions for their rendered response while executing another action. You can either delegate the entire response rendering or you can mix a partial response in with your other content.

class WeblogController < ActionController::Base
  # Performs a method and then lets hello_world output its render
  def delegate_action
    do_other_stuff_before_hello_world
    render_component :controller => "greeter",  :action => "hello_world", :params => { :person => "david" }
  end
end

class GreeterController < ActionController::Base
  def hello_world
    render :text => "#{params[:person]} says, Hello World!"
  end
end

The same can be done in a view to do a partial rendering:

Let's see a greeting: 
<%= render_component :controller => "greeter", :action => "hello_world" %>

It is also possible to specify the controller as a class constant, bypassing the inflector code to compute the controller class at runtime:

<%= render_component :controller => GreeterController, :action => “hello_world” %>

When to use components

Components should be used with care. They're significantly slower than simply splitting reusable parts into partials and conceptually more complicated. Don't use components as a way of separating concerns inside a single application. Instead, reserve components to those rare cases where you truly have reusable view and controller elements that can be employed across many applications at once.

So to repeat: Components are a special-purpose approach that can often be replaced with better use of partials and filters.

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/action_controller/components.rb', line 38

def self.included(base) #:nodoc:
  base.send :include, InstanceMethods
  base.extend(ClassMethods)

  base.helper do
    def render_component(options) 
      @controller.send(:render_component_as_string, options)
    end
  end
        
  # If this controller was instantiated to process a component request,
  # +parent_controller+ points to the instantiator of this controller.
  base.send :attr_accessor, :parent_controller
  
  base.class_eval do
    alias_method :process_cleanup_without_components, :process_cleanup
    alias_method :process_cleanup, :process_cleanup_with_components
    
    alias_method :set_session_options_without_components, :set_session_options
    alias_method :set_session_options, :set_session_options_with_components
    
    alias_method :flash_without_components, :flash
    alias_method :flash, :flash_with_components

    alias_method :component_request?, :parent_controller       
  end
end