Skip to content

Instantly share code, notes, and snippets.

@cdimartino
Last active February 12, 2016 22:33
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 cdimartino/d15117e6bb8deea8b1fa to your computer and use it in GitHub Desktop.
Save cdimartino/d15117e6bb8deea8b1fa to your computer and use it in GitHub Desktop.
Guard clauses!

What happens if the update or create fails? We need to ensure that our user is notified in case of failure:

Anti-pattern

post '/posts' do
  @post = Post.new(params[:post)
  @post.save
  redirect "/posts"
end

Anti-Anti-Pattern:

post '/posts' do
  @post = current_user.posts.build(params[:post)
  if @post.save
    redirect "/posts"
  else
    render :new, locals: { messages: [ "There was an error!", @post.errors.full_messages ] }
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment