Everybody loves the goodness of respond_to, which encapsulates the mime type of the request into a nice little structure, but there are other places where querying against the request mime-type is useful. For instance, applying different filter behaviors depending on the request type is often useful. Starting today you now have a convenient way of doing so with the new request.format.html?, request.format.xml? etc… convenience methods.
1 2 3 4 5 6 7 8 9 |
class MyController < ApplicationController # Toggle authentication method based on request type before_filter do |c| c.use_session_auth if c.request.format.html? c.use_token_auth if c.request.format.xml? end end |
Or you can use these methods to provide a clean way of turning sessions off (from the changeset example):
1 2 3 4 5 6 |
class MyController < ApplicationController # Turn off sessions for all non-html requests sessions :off, :if => Proc.new { |request| not request.format.html? } end |
All in all a tidy way to get at that pesky but deterministic request mime-type. These aren’t just limited to the stock recognized mime-types, they can also be used for any mime-types you’ve registered yourself:
1 2 3 4 5 6 7 8 9 |
# Because I think we all agree I deserve my own mime-type Mime::Type.register "text/ryan", :ryan class MyController < ApplicationController # Turn off sessions for all ryan requests sessions :off, :if => Proc.new { |request| request.format.ryan? } end |
tags: rubyonrails, rails

Anybody want to help me figure out why I have that weird block after the last line of every code snippet? (And why there is no spacing after any of the code snippets?)
CSS will be the death of me someday. Smothered in my sleep, I know it.
I was able to get rid of the little blue button at the bottom of the code blocks by disabling the overflow: auto; from #content pre
and I was able to add some space underneath each code block by adding a margin-bottom: 5px; in the table.CodeRay block of your css.
haven’t tested those changes in other browsers though so you might want to check that I’m not suggesting making things worse. It’s been awhile since I’ve done any serious CSS styling.
I have no helpful suggestions, but I did almost spit my drink out when I saw your comment about being smothered to death by CSS.
It’s funny because it’s true, CSS is the devil. It lures you in with it’s sweet promises and then leaves you awake at 3am trying to assign a margin consistently across 3 browsers.
CSS is the directly revealed word of God. It’s the browsers that are fallen.
Eldon – your tweaks were perfect, thank you all for the tips!