Posted by jcarroll
Feb
22
Ever write this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
before_filter :csv?, :only => :index
def index
respond_to do |format|
format.html { @users = User.all }
format.csv { @users = User.to_csv }
end
end
def csv?
if request.format == :csv && ! logged_in?
log_in
end
end |
This would be a lot nicer if we could get rid of that #== in #csv?
1
2
3
4
5
|
def csv?
if request.format.csv? && ! logged_in?
log_in
end
end |
Introducing What
What is a plugin that automatically adds those nice query methods to ActionController::Mime::Type, the type of object returned by Request#format. All the built in mime types are supported. Any custom mime types that you add will also have corresponding query methods.
Comments on this post
Feb 22
Dan said,
This is a pretty nice idiom. Its probably worth submitting as a patch to Rails, since I think it much cleaner than using == to test the format type.
Feb 23
Pratik said,
That’s a nice idea ! In fact, why not just request.csv? instead of request.format.csv? ?
Feb 25
theJareCare said,
@ pratik
i thought about that but decided not to because that request object already has #get?, #post?, #xhr?. so i looked at it as the type of request being the HTTP verb and the type of the request’s format being something like #csv? or #vista_phone?
Sorry, comments are closed for this article.
© 2000 - 2009 by thoughtbot, inc.
written by a bushel of tiny robots
Come “ride the toad” on Hoptoad, the app error app.
Thunder Thimble: Brand monitoring for social media.
Widgetfinger: Simple content management for simple websites.
Tee-Bot, funny shirts your friends won't understand!
Umbrella Today: “It’s like totally the simplest weather report ever, Julie.”
Thoughtbot
thoughtbot is a technology consulting firm that provides web application development and design services. We focus on building modern systems, embracing good ideas and delivering elegant solutions.
Interested in learning Rails?
Sign up for our beginning or advanced training.
Archives