Last active
October 2, 2015 03:18
-
-
Save alexch/2160836 to your computer and use it in GitHub Desktop.
unbloating Rails' respond_to scaffolding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# why does Rails generate this... | |
def index | |
@users = User.all | |
respond_to do |format| | |
format.html # index.html.erb | |
format.json { render json: @users } | |
end | |
end | |
# ...and not something like this? | |
def index | |
@users = User.all | |
case params[:format] | |
when 'html' | |
# index.html.erb | |
when 'json' | |
render json: @users | |
end | |
end | |
end | |
# ...or -- because 'html' is a no-op -- this? | |
def index | |
@users = User.all | |
render json: @users if params[:format] == "json" | |
end | |
# or (assuming a new method named "format_as?") | |
def index | |
@users = User.all | |
render json: @users if format_as?(:json) | |
end | |
# The whole "respond_to" thing makes learning and using Rails harder | |
# than it needs to be. It smells like it was written by someone who | |
# just learned about blocks and really wanted an excuse to use them. | |
# TL;DR: Rails Hates Ruby |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment