What are default respond_to formats for Ruby On Rails and how to go about adding new ones?
-
19-09-2019 - |
Question
So here 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 possibly some custom ones such as one that returns the image for the profile.
Solution
You can register new ones like this (place this in your config/environment.rb, one of the config/environments/*.rb files or in a file under config/initializers):
Mime::Type.register 'application/pdf', :pdf
Mime::Type.register 'application/vnd.ms-excel', :xls
As for the default ones:
>> Mime::SET.map(&:to_sym)
=> [:all, :text, :html, :js, :css, :ics, :csv, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :xls]
OTHER TIPS
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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow