class ActionController::Responder
Check polymorphic_url
documentation for more examples.
respond_with(@project, :manager, @task)
should be invoked as:
polymorphic urls. If a project has one manager which has many tasks, it
Namespaced and singleton resources require a symbol to be given, as inproject_task_url
instead of task_url
.
Giving an array of resources, you ensure that the responder will redirect to
end
respond_with(@project, @task)<br>flash = ‘Task was successfully created.’ if @task.save
@task = @project.comments.build(params)
@project = Project.find(params)
def create
TasksController would be like:
Consider the project has many tasks example. The create action for
You can supply nested resources as you do in form_for
and polymorphic_url
.
=== Nested resources
The same happens for PUT and DELETE requests.
end
end
end
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
format.html { render :action => “new” }
else
format.xml { render :xml => @user, :status => :created, :location => @user }
format.html { redirect_to(@user) }
= ‘User was successfully created.’
if @user.save
respond_to do |format|
@user = User.new(params)
def create
Which is exactly the same as:<br><br>end<br>respond_with(@user)<br>flash = ‘User was successfully created.’ if @user.save
@user = User.new(params)
def create
be written as:
Using Rails default responder, a POST request for creating an object could
content type, verb and the resource status, it will behave differently.
The default Rails responder holds semantics for each HTTP verb. Depending on the
=== Builtin HTTP verb semantics
3) if the responder does not respond_to :to_xml
, call #to_format
on it.
2) if the template is not available, it will invoke #to_xml
on the given resource;
1) the responder searches for a template at people/index.xml;
When a request comes in, for example for an XML response, three steps happen:
end
end
respond_with(@people)
@people = Person.find(:all)
def index
respond_to :html, :xml, :json
class PeopleController < ApplicationControllerrespond_with
is called. The simplest case to study is a GET request:
usually depending on the HTTP verb. The responder is triggered when
Responder is responsible for exposing a resource to different mime requests,
:nodoc:
def self.call(*args)
not defined, call to_format.
Initializes a new responder an invoke the proper format. If the format is
def self.call(*args) new(*args).respond end
def api_behavior(error)
def api_behavior(error) raise error unless resourceful? if get? display resource elsif has_errors? display resource.errors, :status => :unprocessable_entity elsif post? display resource, :status => :created, :location => api_location elsif has_empty_resource_definition? display empty_resource, :status => :ok else head :ok end end
def default_action
the verb is POST.
By default, render the
:edit
action for HTML requests with failure, unless
def default_action @action ||= ACTIONS_FOR_VERBS[request.request_method_symbol] end
def default_render
controller.
If a given response block was given, use it, otherwise call render on
def default_render @default_response.call end
def display(resource, given_options={})
render :xml => @user, :status => :created
Results in:
display(@user, :status => :ok)
respond_with(@user, :status => :created)
Options sent by the user are also used:
render :xml => @user, :status => :ok
For XML requests it's equivalent to:
display @user, :status => :ok
Display is just a shortcut to render a resource with the current format.
def display(resource, given_options={}) controller.render given_options.merge!(options).merge!(format => resource) end
def empty_json_resource
Return a valid empty JSON resource
def empty_json_resource "{}" end
def empty_resource
Delegate to proper empty resource method
def empty_resource send("empty_#{format}_resource") end
def has_empty_resource_definition?
Check whether resource needs a specific definition of empty resource to be valid
def has_empty_resource_definition? respond_to?("empty_#{format}_resource") end
def has_errors?
Check whether the resource has errors.
def has_errors? resource.respond_to?(:errors) && !resource.errors.empty? end
def initialize(controller, resources, options={})
def initialize(controller, resources, options={}) @controller = controller @request = @controller.request @format = @controller.formats.first @resource = resources.last @resources = resources @options = options @action = options.delete(:action) @default_response = options.delete(:default_response) end
def navigation_behavior(error)
def navigation_behavior(error) if get? raise error elsif has_errors? && default_action render :action => default_action else redirect_to navigation_location end end
def resource_location
returning the resources array.
Returns the resource location by retrieving it from the options or
def resource_location options[:location] || resources end
def resourceful?
Checks whether the resource responds to the current format or not.
def resourceful? resource.respond_to?(:"to_#{format}") end
def respond
Main entry point for responder responsible to dispatch to the proper format.
def respond method = :"to_#{format}" respond_to?(method) ? send(method) : to_format end
def to_format
responds to :to_format and display it.
template, if the template is not available, we verify if the resource
All other formats follow the procedure below. First we try to render a
def to_format default_render rescue ActionView::MissingTemplate => e api_behavior(e) end
def to_html
template.
HTML format does not render the resource, it always attempt to render a
def to_html default_render rescue ActionView::MissingTemplate => e navigation_behavior(e) end