Skip to content

Instantly share code, notes, and snippets.

@alexch
Last active October 2, 2015 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexch/2160836 to your computer and use it in GitHub Desktop.
Save alexch/2160836 to your computer and use it in GitHub Desktop.
unbloating Rails' respond_to scaffolding
# 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