module RailsAjax::Controller

Module defining new methods that will be part of every controller

Public Instance Methods

redirect_to(options = {}, response_status = {}) click to toggle source

Render a redirection Adapt to AJAX calls

Calls superclass method
# File lib/rails-ajax/controller.rb, line 52
def redirect_to(options = {}, response_status = {})
  if (RailsAjax.config.enabled? and request.xhr?)
    logger.debug "[RailsAjax] redirect_to: options=#{options.inspect} response_status=#{response_status.inspect}"
    # Use 'application/json'
    self.content_type = 'application/json'
    self.response_body = get_json_response(
      :redirect_to => url_for(options)
    ).to_json
  else
    super(options, response_status)
  end
end
render(*options, &block) click to toggle source

Render Adapt to AJAX calls, by returning the following JSON object that will be interpreted by client side JavaScript.

Calls superclass method
# File lib/rails-ajax/controller.rb, line 8
def render(*options, &block)
  if (RailsAjax.config.enabled?)
    args = _normalize_args(*options, &block)
    if ((request.xhr?) and
        (!args.has_key?(:partial)) and
        (!args.has_key?(:layout)) and
        (!args.has_key?(:json)) and
        (params['format'] != 'json') and
        (self.content_type != 'application/json'))
      logger.debug "[RailsAjax] render: options=#{options.inspect} block?#{block != nil} flash=#{flash.inspect} | Normalized arguments: #{args.inspect}"

      # If we have a redirection, use redirect_to
      if (args[:location] == nil)
        # Complete arguments if needed
        # We don't want a special layout for Ajax requests: this was asked using AJAX for a page to be displayed in the main content
        args[:layout] = false
        # Render
        main_content = render_to_string(args, &block)

        # Send JSON result
        # Use 'application/json'
        self.content_type = 'application/json'
        self.response_body = get_json_response(
          :elements_to_refresh => {
            RailsAjax.config.main_container => main_content
          }
        ).to_json
      elsif (args[:status] == nil)
        redirect_to args[:location]
      else
        redirect_to args[:location], args[:status]
      end

    else
      super(*options, &block)
    end
  else
    super(*options, &block)
  end

end

Protected Instance Methods

execute_javascript(js_code) click to toggle source

Add a Javascript to be executed just after the Ajax call This is used to execute special Ajax handling that is not needed in case the same request is made without Ajax

Parameters
  • js_code (String): Javascript to be executed

# File lib/rails-ajax/controller.rb, line 85
def execute_javascript(js_code)
  if RailsAjax.config.enabled?
    logger.debug "[RailsAjax] Add javascript to be executed: #{js_code[0..255]}"
    @js_to_execute = [] if (defined?(@js_to_execute) == nil)
    @js_to_execute << js_code
  end
end
refresh_dom_with_partial(css_selector, partial_name) click to toggle source

Mark given DOM elements (selected using a CSS selector) to be refreshed with a partial's content

Parameters
  • css_selector (String): The CSS selector to be used to refresh elements

  • partial_name (String): Name of the partial to be used to refresh these elements

# File lib/rails-ajax/controller.rb, line 72
def refresh_dom_with_partial(css_selector, partial_name)
  if RailsAjax.config.enabled?
    logger.debug "[RailsAjax] Mark partial #{partial_name} to be refreshed in #{css_selector}"
    @partials_to_refresh = {} if (defined?(@partials_to_refresh) == nil)
    @partials_to_refresh[css_selector] = partial_name
  end
end