When a nil
user clicks "sign up" he is encouraged to submit a goal, habit, and value before being logged in.
But he may skip one of those forms by clicking:
The problem is if he skips a form right now then an error message pops up:
TypeError in UsersController#create
no implicit conversion of nil into String
In this example I skipped the habit form so it is being raised on @user.habits.create(
users_controller
def create
@user = User.new(user_params)
if @user.save
# Goals
name = session.delete(:goal_name)
deadline = session.delete(:goal_deadline)
# Values
vname = session.delete(:valuation_name)
vimage = session.delete(:valuation_image)
# Habits
date_started = session.delete(:habit_date_started)
committed = session.delete(:habit_committed)
trigger = session.delete(:habit_trigger)
action = session.delete(:habit_action)
target = session.delete(:habit_target)
reward = session.delete(:habit_reward)
missed_days = session.delete(:habit_missed_days)
# Create
@user.habits.create(date_started: Date.parse(date_started), committed: committed, trigger: trigger, action: action, target: target, reward: reward, missed_days: missed_days).create_with_current_level
@user.goals.create(name: name, deadline: deadline)
@user.valuations.create(name: vname, image: vimage)
@user.send_activation_email
redirect_to root_url
else
render 'new'
end
end
How can we allow the user to skip? I've been trying to include various if/else
statements, but I'm having a hard time setting the right method to trigger the conditional.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…