What are the default response_to formats for Ruby on Rails and how do I add new ones?

So, this is what I have:

  def index
    @profiles = Profile.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @profiles }
      format.json  { render :json => @profiles }
    end
  end

      

I would like to add rss, atom and maybe some custom ones like those that return a picture for the profile.

+2


source to share


2 answers


You can register new ones like this (put this in your config / environment.rb, one of the config / environment / *. Rb files, or a file in config / initializers):

Mime::Type.register 'application/pdf', :pdf
Mime::Type.register 'application/vnd.ms-excel', :xls

      



As for the standard values:

>> Mime::SET.map(&:to_sym)
=> [:all, :text, :html, :js, :css, :ics, :csv, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :xls]

      

+6


source


You can use the resource_feeder built into rails to do this:

script/plugin install simply_helpful
script/plugin install resource_feeder

      

In the profile controller:



def index
  @profiles = Profile.all

  options = { :feed => { :title => "All Profiles" },
                    :item => { :title => :name } }

  respond_to do |format|
    format.html
    format.xml { render :xml => @profiles
    format.json { render :json => @profiles
    format.rss { render_rss_feed_for @profiles, options }
    format.atom { render_atom_feed_for @profiles, options }
  end

      

end

0


source







All Articles