RoR - Two reply_to formats using the same block?

Is there something like:

respond_to do |format|

  format.html || format.xml do
    #big chunk of code
  end

end

I would like to do it for DRY.

+5
source share
3 answers

Respond_to actually allows you to specify your common block for different formats, using any:

format.any(:js, :json) { #your_block }
+22
source

You can use this format:

class PeopleController < ApplicationController
  respond_to :html, :xml, :js

  def index
    @people = Person.find(:all)
    respond_with(@people) do |format|
        format.html
        format.xml
        format.js { @people.custom_code_here }
    end
  end
end

To achieve what you are looking for, if you have a more difficult situation, let me know. See the article in the article on the response_with method for more information .

+4
source

respond_to do |format|
  format.html do
    #block
  end
  format.xml do
    #block
  end
end

respond_to do |format|
  format.html { #block }
  format.xml { #block }
end

ruby ​​blocks, Procs.

respond_to do |format|
  bcoc = Proc.new do
    # your big chunk of code here
  end
  format.html bcoc
  format.xml bcoc
end

, , ?

+1

All Articles