How to save a user's goal in his session? Then once he signed up, save that goal and delete it from the session?
On the home page it looks like this:
goals/_form.html.erb
<%= simple_form_for(@goal) do |f| %>
<%= f.text_field :name %>
<%= f.text_field :deadline %>
<%= f.submit %>
<% end %>
Upon clicking submit, the nil
user's goal should be added to his session and then he should be redirected to the signup page whereupon signing up his goal should be added to his newly created account.
goals_controller
def create
@goal = params[:user] ? current_user.goals.build(goal_params) : Goal.new(goal_params)
if params[:session][:name][:deadline]
session[:name][:deadline] = params[:session][:name][:deadline]
redirect_to signup_url
elsif
@goal.save
track_activity @goal
redirect_to @goal, notice: 'Goal was successfully created'
else
flash.now[:danger] = 'Required Field: "Enter Goal"'
render 'new'
end
end
Right now if I click submit as a nil
user I get:
NoMethodError in GoalsController#create
undefined method `[]' for nil:NilClass
for line: if params[:session][:name][:deadline]
Then once he signed up the goal stored in his session should be added to his account. If this last condition is too complex I can make it a separate question.