smatthewenglish
smatthewenglish

Reputation: 2889

Sending confirmation email with SendGrid on Rails - am I saving user data?

The boilerplate code from the Rails tutorial of SendGrid looks like this:

class UsersController < ApplicationController
  def create
    # Create the user from params
    @user = User.new(params[:user])
    if @user.save
      # Deliver the signup email
      UserNotifier.send_signup_email(@user).deliver
      redirect_to(@user, :notice => 'User created')
    else
      render :action => 'new'
    end
  end
end

but for me the like redirect_to(@user, :notice => 'User created') was causing problems- I guess because my own database was not set up in that way.

Then what I did was replace it by render :new unless @user.save, because that's what my old create method looked like, i.e. it looked like this:

# def create
#   @user = User.new(user_params)
#   render :new unless @user.save
# end

Now my function looks like so:

def create
  # Create the user from params
  @user = User.new(user_params)
  if @user.save
    # Deliver the signup email
    UserNotifierMailer.send_signup_email(@user).deliver
    # redirect_to(@user, :notice => 'User created')
    render :new unless @user.save
  else
    # render :action => 'new'
    render :new unless @user.save
    # is putting "render :new unless @user.save" detrimental? does it
    # actually solve the problem of saving that user?
  end
end

Is that reasonable? I mean - admittedly it's kind of hacky - is it bad though?

Upvotes: 0

Views: 108

Answers (1)

Oleh  Sobchuk
Oleh Sobchuk

Reputation: 3722

use redirect after create object:

def create
  # Create the user from params
  @user = User.new(user_params)
  if @user.save
    # Deliver the signup email
    UserNotifierMailer.send_signup_email(@user).deliver
    redirect_to new_user_path # or another path what you wish
  else
    render :new
  end
end

if you use render you render only view, if redirect_to - you go to view through action

Upvotes: 1

Related Questions