HiveBrain v1.2.0
Get Started
← Back to all entries
patternrubyrailsMinor

Return render in Ruby on Rails

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
returnrailsrenderruby

Problem

Is it bad to do return render? I am doing this to NOT also allow a redirect.

def create
    @patient = Patient.new(params[:patient])
    if  !@patient.save
      @patient.user.password = ''
      @patient.user.password_confirmation = ''
      return render is_admin? ? 'new_admin' : 'new'
    else
      #Fire off an e-mail
      PatientMailer.welcome_email(@patient).deliver
    end

    if current_user == @patient
      sign_in @patient
    else
      redirect_to patients_path
    end
  end

Solution

Writing it like this makes it look the render returns a meaningful value that is then returned by create and used by the code that calls create, which is not the case. So instead I would write:

render is_admin? ? 'new_admin' : 'new'
return


This makes it clear that render is solely used for its side effects and create does not return a value (other than nil).

Code Snippets

render is_admin? ? 'new_admin' : 'new'
return

Context

StackExchange Code Review Q#996, answer score: 8

Revisions (0)

No revisions yet.