How to set the default format for a route in Rails? -


with default routing, request /posts/:id gets mapped "show" action :format => "html". using xhtml elements in show action don't rendered correctly unless :content_type set xml. getting around rendering show.xml.erb , setting content_type manually follows:

format.html { render :template => "/posts/show.xml.erb",               :locals => {:post => @post}, :content_type => "text/xml" } 

this seems silly though. how can change routes.rb /posts/:id routed format=>"xml"? thanks.

default format requests:

you can set default format of given route xml using defaults hash.

examples:

# single match defaulting xml (/plots/1 same /plots/1.xml) match 'posts/:id' => 'posts#show', :defaults => { :format => 'xml' }  # using resources, defaulting xml (all action use xml default) resources :posts, :defaults => { :format => 'xml' }  # using resources , mixing other options resources :posts,           :only => [:new, :create, :destroy],           :defaults => { :format => 'xml' } 

it's idea search official ruby on rails routing guide, it's in-depth , first-stop resource routing issues.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -