Skip to content

Instantly share code, notes, and snippets.

@justinko
Created October 17, 2012 22:48
Show Gist options
  • Save justinko/3908831 to your computer and use it in GitHub Desktop.
Save justinko/3908831 to your computer and use it in GitHub Desktop.
class PostsController
def index
@posts = Post.fetch_all
rescue Post::FetchError
redirect_to root_url
end
def recent
@posts = Post.fetch_recent
render 'index'
rescue Post::FetchError
redirect_to :back
end
end
# VS
class PostsController
# Since the error happens in template rendering, have to use `rescue_from`
# This results in a loss of context, IMO
rescue_from Post::FetchError do
case action_name
when 'index'
redirect_to root_url
when 'recent'
redirect_to :back
end
end
def index
respond_with ViewModel::Posts.new
end
def recent
# Would render the "index" template, somehow
respond_with ViewModel::RecentPosts.new
end
end
class ViewModel::Posts
def posts
Post.fetch_all
end
end
class ViewModel::RecentPosts
def posts
Post.fetch_recent
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment