Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

ruby on rails - How to skip a session create?

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:

enter image description here

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am going to assume that if they submit the form all the values get populated. That said:

users_controller

  def create
    @user = User.new(user_params)
    make_habit = false
    make_goal = false
    make_valuation = false

    if @user.save
      # Goals
      name = session.delete(:goal_name)
      deadline = session.delete(:goal_deadline)
      make_goal = true  if !name.blank? && !deadline.blank?

      # Values
      vname = session.delete(:valuation_name)
      vimage = session.delete(:valuation_image)
      make_valuation = true  if !vname.blank? && !vimage.blank?

      # 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)
      make_habit = true  if !date_started.blank? && !committed.blank? #(... continue for whatever else you want to make sure is there)

      # 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  if make_habit
      @user.goals.create(name: name, deadline: deadline)  if make_goal
      @user.valuations.create(name: vname, image: vimage)  if make_valuation
      @user.send_activation_email
      redirect_to root_url
    else
      render 'new'
    end
  end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...