module ActionController::MimeResponds
def respond_to(*mimes)
format.html.phone # this gets rendered
format.html.none
respond_to do |format|
is no +:tablet+ variant declared, the +:phone+ variant will be used:
This will work similarly to formats and MIME types negotiation. If there
request.variant = [:tablet, :phone]
You can also set an array of variants:
end
end
variant.phone { render html: "phone" }
variant.any(:tablet, :phablet){ render html: "any" }
format.html do |variant|
respond_to do |format|
and block syntax:
end
format.html.phone { render html: "phone" }
format.html.any { render html: "any" }
respond_to do |format|
It works for both inline:
Variants also support common +any+/+all+ block that formats have.
end
format.html.none { render "trash" }
format.html.phone { redirect_to progress_path }
format.js { render "trash" }
respond_to do |format|
using the inline syntax:
When you're not sharing any code within the format, you can simplify defining variants
app/views/projects/show.html+phone.erb
app/views/projects/show.html+tablet.erb
app/views/projects/show.html.erb
Provide separate templates for each format and variant:
end
end
variant.none { special_setup } # executed only if there is no variant set
variant.phone { extra_setup; render ... }
variant.tablet # renders app/views/projects/show.html+tablet.erb
format.html do |variant|
respond_to do |format|
Respond to variants in the action just like you respond to formats:
request.variant = :tablet if /iPad/.match?(request.user_agent)
You can set the variant in a +before_action+:
tablets, and desktop browsers. Variants make it easy.
We often want to render different html/json/xml templates for phones,
:phone, or :desktop.
The request variant is a specialization of the request format, like :tablet,
Formats can have different variants.
end
format.any { redirect_to support_path }
format.html
respond_to do |format|
the user:
+any+ can also be used with no arguments, in which case it will be used for any format requested by
render json: @people
Or if the format is json:
render xml: @people
In the example above, if the format is xml, it will render:
end
end
format.any(:xml, :json) { render request.format.to_sym => @people }
format.html
respond_to do |format|
@people = Person.all
def index
+respond_to+ also allows you to specify a common block for different formats by using +any+:
Mime::Type.register "image/jpeg", :jpg
+config/initializers/mime_types.rb+ as follows.
If you need to use a MIME type which isn't supported by default, you can register your own handlers in
and accept Rails' defaults, life will be much easier.
in a single request (i.e., by wrapping them all in a single root node), but if you just go with the flow
Note that you can define your own XML parameter parser which would allow you to describe multiple entities
with the remaining data.
we extract the company data from the request, find or create the company, and then create the new person
In other words, we make the request so that it operates on a single entity's person. Then, in the action,
And, like this (xml-encoded):
person[name]=...&person[company][name]=...&...
single root-node. So, we have to rearrange things so that the request looks like this (url-encoded):
This is because the incoming XML document (if a web-service request is in process) can only contain a
@company = Company.find_or_create_by(name: company[:name])
company = params[:person].delete(:company)
Note, however, the extra bit at the top of that action:
...
...
include the person's company in the rendered XML, so you get something like this:
Lastly, if the client wants XML, we render the created person as XML, but with a twist: we also
then it is an Ajax request and we render the JavaScript template associated with this action.
If the client wants HTML, we just redirect them back to the person list. If they want JavaScript,
end
end
format.xml { render xml: @person.to_xml(include: @company) }
format.js
format.html { redirect_to(person_list_url) }
respond_to do |format|
@person = @company.people.create(params[:person])
@company = Company.find_or_create_by(name: company[:name])
company = params[:person].delete(:company)
def create
Here's the same action, with web-service support baked in:
end
redirect_to(person_list_url)
@person = @company.people.create(params[:person])
@company = Company.find_or_create_by(name: params[:company][:name])
def create
(by name) if it does not already exist, without web-services, it might look like this:
Supposing you have an action that adds a new person, optionally creating their company
(Rails determines the desired response format from the HTTP Accept header submitted by the client.)
would have before, but if the client wants XML, return them the list of people in XML format."
What that says is, "if the client wants HTML or JS in response to this action, just respond as we
end
end
format.xml { render xml: @people }
format.js
format.html
respond_to do |format|
@people = Person.all
def index
Here's the same action, with web-service support baked in:
end
respond_to :html, :js
@people = Person.all
def index
That action implicitly responds to all formats, but formats can also be explicitly enumerated:
end
@people = Person.all
def index
might look something like this:
Without web-service support, an action which collects the data for displaying a list of people
def respond_to(*mimes) raise ArgumentError, "respond_to takes either types or a block, never both" if mimes.any? && block_given? collector = Collector.new(mimes, request.variant) yield collector if block_given? if format = collector.negotiate_format(request) if media_type && media_type != format raise ActionController::RespondToMismatchError end _process_format(format) _set_rendered_content_type(format) unless collector.any_response? response = collector.response response.call if response else raise ActionController::UnknownFormat end end