问题描述
如何在用户的会话中保存用户的目标?然后,一旦他注册,保存该目标并将其从会话中删除?
在主页上是这样的:
goals/_form.html.erb
点击提交后,nil
用户的目标应该被添加到他的会话中,然后他应该被重定向到注册页面,在那里注册他的目标应该被添加到他新创建的帐户中.>
goals_controller
def 创建@goal = params[:user] ?current_user.goals.build(goal_params) : Goal.new(goal_params)如果参数[:session][:name][:deadline]session[:name][:deadline] = params[:session][:name][:deadline]重定向到 signup_url埃尔西夫@goal.savetrack_activity @goalredirect_to @goal,注意:'目标已成功创建'别的flash.now[:danger] = '必填字段:输入目标"'呈现新"结尾结尾
现在,如果我以 nil
用户的身份点击提交,我会得到:
GoalsController#create 中的 NoMethodErrornil:NilClass 的未定义方法`[]'对于行:if params[:session][:name][:deadline]
然后,一旦他注册了他的会话中存储的目标,就应该将其添加到他的帐户中.如果最后一个条件太复杂,我可以单独提出一个问题.
目标控制器
def 创建除非 params[:user].present?# 如果没有用户,则将目标值存储到会话中session[:goal_name] =goal_params[:name]session[:goal_deadline] =goal_params[:deadline]重定向到 signup_url别的@goal = current_user.goals.build(goal_params)如果@goal.savetrack_activity @goalredirect_to @goal,注意:'目标已成功创建'别的flash.now[:danger] = '必填字段:输入目标"'呈现新"结尾结尾
用户控制器
def 创建# 创建用户逻辑如果@user.save# 抓取会话变量的同时删除它gName = session.delete(:goal_name)gDeadline = session.delete(:goal_deadline)# 您可以使错误处理更复杂@user.goals.create(名称:gName,截止日期:gDeadline)重定向到成功创建路径别的...结尾结尾
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.
Goals Controller
def create
unless params[:user].present?
# If there is no user, store the goal values to the session
session[:goal_name] = goal_params[:name]
session[:goal_deadline] = goal_params[:deadline]
redirect_to signup_url
else
@goal = current_user.goals.build(goal_params)
if @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
User Controller
def create
# create user logic
if @user.save
# Grab the session variable at the same time deleting it
gName = session.delete(:goal_name)
gDeadline = session.delete(:goal_deadline)
# You can make this more complex for error handling
@user.goals.create(name: gName, deadline: gDeadline)
redirect_to success_creation_path
else
...
end
end
这篇关于如何在会话中存储 nil 用户的目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!