instance method
respond_with
Ruby on Rails 3.1.12
Since v3.1.12 Last seen in v4.1.16Signature
respond_with(*resources, &block)
respond_with wraps a resource around a responder for default representation. First it invokes respond_to, if a response cannot be found (ie. no block for the request was given and template was not available), it instantiates an ActionController::Responder with the controller and resource.
Example
def index @users = User.all respond_with(@users) end
It also accepts a block to be given. It’s used to overwrite a default response:
def create @user = User.new(params[:user]) flash[:notice] = "User was successfully created." if @user.save respond_with(@user) do |format| format.html { render } end end
All options given to respond_with are sent to the underlying responder, except for the option :responder itself. Since the responder interface is quite simple (it just needs to respond to call), you can even give a proc to it.
In order to use respond_with, first you need to declare the formats your controller responds to in the class level with a call to respond_to.
Parameters
-
resourcesrest -
blockblock
Source
# File actionpack/lib/action_controller/metal/mime_responds.rb, line 231
def respond_with(*resources, &block)
raise "In order to use respond_with, first you need to declare the formats your " <<
"controller responds to in the class level" if self.class.mimes_for_respond_to.empty?
if response = retrieve_response_from_mimes(&block)
options = resources.size == 1 ? {} : resources.extract_options!
options.merge!(:default_response => response)
(options.delete(:responder) || self.class.responder).call(self, resources, options)
end
end
Defined in actionpack/lib/action_controller/metal/mime_responds.rb line 231
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActionController::MimeResponds