Skip to content

Instantly share code, notes, and snippets.

@stevepolitodesign
Last active December 7, 2021 11:02
Show Gist options
  • Save stevepolitodesign/6874d8ebbd22f54cfce435ad1972a72d to your computer and use it in GitHub Desktop.
Save stevepolitodesign/6874d8ebbd22f54cfce435ad1972a72d to your computer and use it in GitHub Desktop.
Should you return a status when calling render in a Controller?

Before

class PostsController < ApplicationController
  def create
    if @post.save(post_params)
      redirect_to @post
    else
      render :new
    end
  end
end

After

class PostsController < ApplicationController
  def create
    if @post.save(post_params)
      redirect_to @post
    else
      render :new, status: :unprocessable_entity
    end
  end
end

Rails Scaffold

  # POST /posts
  def create
    @post = Post.new(post_params)

    if @post.save
      redirect_to @post, notice: 'Post was successfully created.'
    else
      render :new
    end
  end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment