如何使用“save_with_current_level"保存会话? [英] How to save session with "save_with_current_level"?
问题描述
一旦 nil
用户提交习惯,它应该保存到会话中,然后在用户创建帐户后保存,如 @blnc 能够帮助我让它为目标工作.>
虽然它对习惯不起作用,我认为这是因为我想将 save_with_current_level
添加到它,就像我在 习惯中由实际用户保存时所做的那样s_controller.
def 创建如果 current_user == nil# 如果没有用户,则将目标值存储到会话中.session[:habit_committed] = 习惯参数[:committed =>[]]会话[:habit_date_started] = 习惯参数[:date_started]会话[:habit_trigger] = 习惯参数[:trigger]会话[:习惯动作] = 习惯参数[:动作]会话[:习惯目标] = 习惯参数[:目标]会话[:habit_reward] = 习惯参数[:reward]会话[:习惯顺序] = 习惯参数[:顺序]会话[:habit_missed_days] = 习惯参数[:missed_days]重定向到 signup_url别的@habit = current_user.habits.build(habit_params)如果@habit.conceal == true@habit.save_with_current_levelredirect_to @habit,注意:'习惯已成功创建.请记住,如果您错过了承诺"的一天,请给自己一个罢工.3 次罢工,您当前的级别将重新启动.祝你好运!'埃尔西夫@habit.save_with_current_leveltrack_activity @habitredirect_to @habit,注意:'习惯已成功发布.请记住,如果您错过了承诺"的一天,请给自己一个罢工.3 次罢工,您当前的级别将重新启动.祝你好运!'别的flash.now[:danger] = '必填字段:致力于"、开始"和进入习惯"'呈现新"结尾结尾结尾
users_controller
def 创建@user = User.new(user_params)如果@user.save# 抓取会话变量的同时删除它名称 = session.delete(:goal_name)截止日期 = session.delete(:goal_deadline)vname = session.delete(:valuation_name)vimage = session.delete(:valuation_image)提交 = session.delete(:habit_committed)date_started = session.delete(:habit_date_started)触发器 = session.delete(:habit_trigger)action = session.delete(:habit_action)目标 = session.delete(:habit_target)奖励 = session.delete(:habit_reward)order = session.delete(:habit_order)错过的天数 = session.delete(:habit_missed_days)# 我相信这是因为下面的行没有 save_with_current_level@user.habits.create(committed: commited, date_started: date_started, trigger: trigger, action: action, target: target, Reward: Reward, order: order,missed_days:missing_days).save_with_current_level@user.goals.create(名称:姓名,截止日期:截止日期)@user.valuations.create(name: vname, image: vimage)@user.send_activation_email重定向到 root_url别的呈现新"结尾结尾
habit.rb
班级习惯{ 订单(:id)}before_save :current_leveldef save_with_current_levelself.levels.buildself.levels.buildself.levels.buildself.levels.buildself.levels.build自救结尾def current_habit_levelself.levels.order("id asc").limit(self.current_level).last结尾attr_accessor :missed_one, :missed_two, :missed_threedef完成=(布尔)self.completed_at = 布尔值?时间.current : nil结尾定义完成Completed_at &&Completed_at >= Time.current.beginning_of_day结尾def self.committed_for_todaytoday_name = Date::ABBR_DAYNAMES[Date.today.wday].downcaseids = all.select { |h|h.committed.include?today_name }.map(&:id)哪里(ID:ID)结尾def current_level_strikelevels[current_level - 1] # 记住数组索引从 0 开始结尾定义当前级别返回 0 除非 date_starteddef commited_wdays提交.map 做|天|日期::ABBR_DAYNAMES.index(day.titleize)结尾结尾定义 n_days((date_started.to_date)..Date.yesterday).count do |date|commited_wdays.include?日期.wday结束 - self.real_missed_days结尾案例 n_days当 0..91当 10..242当 25..443当 45..694当 70..995别的6结尾结尾def real_missed_days值 = 0水平.每个做|水平|值 += level.missed_days + level.days_lost结尾价值结尾def calculate_days_lost定义 n_days((date_started.to_date)..Date.yesterday).count do |date|commited_wdays.include?日期.wday结束 - self.real_missed_days结尾案例 n_days当 0..9n_天当 10..24n_days-10当 25..44n_days-25当 45..69n_days-45当 70..99n_days-70别的n_days-100结尾结尾def days_left_in_current_level定义 n_days((date_started.to_date)..Date.yesterday).count do |date|commited_wdays.include?日期.wday结束 - self.real_missed_days结尾案例 n_days当 0..910-n_days当 10..2425-n_days当 25..4445-n_days当 45..6970-n_days当 70..99100-n_days别的0 # 没有结束结尾结尾结尾
我不太确定你的问题是什么,但我认为这又是你没有保存你的关卡.另外你要先保存你的习惯,否则没有id可以分配你的级别
def save_with_current_level自救!5.times {self.levels.create!}结尾
Once a nil
user submits a habit it should be saved to the session and then to the user once he creates an account, as @blnc was able to help me get it to work for goals.
It doesn't work for habit's though and I think it's because I'm suppose to add save_with_current_level
to it as I do when it is saved by an actual user in the habit's_controller.
def create
if current_user == nil
# If there is no user, store the goal values to the session.
session[:habit_committed] = habit_params[:committed => []]
session[:habit_date_started] = habit_params[:date_started]
session[:habit_trigger] = habit_params[:trigger]
session[:habit_action] = habit_params[:action]
session[:habit_target] = habit_params[:target]
session[:habit_reward] = habit_params[:reward]
session[:habit_order] = habit_params[:order]
session[:habit_missed_days] = habit_params[:missed_days]
redirect_to signup_url
else
@habit = current_user.habits.build(habit_params)
if @habit.conceal == true
@habit.save_with_current_level
redirect_to @habit, notice: 'Habit was successfully created. Remember, if you miss a "committed" day give yourself a strike.
3 strikes and your current level restarts. Good luck!'
elsif
@habit.save_with_current_level
track_activity @habit
redirect_to @habit, notice: 'Habit was successfully published. Remember, if you miss a "committed" day give yourself a strike.
3 strikes and your current level restarts. Good luck!'
else
flash.now[:danger] = 'Required Fields: "Committed to", "Started", and "Enter Habit"'
render 'new'
end
end
end
users_controller
def create
@user = User.new(user_params)
if @user.save
# Grab the session variable at the same time deleting it
name = session.delete(:goal_name)
deadline = session.delete(:goal_deadline)
vname = session.delete(:valuation_name)
vimage = session.delete(:valuation_image)
committed = session.delete(:habit_committed)
date_started = session.delete(:habit_date_started)
trigger = session.delete(:habit_trigger)
action = session.delete(:habit_action)
target = session.delete(:habit_target)
reward = session.delete(:habit_reward)
order = session.delete(:habit_order)
missed_days = session.delete(:habit_missed_days)
# I believe it's because the line below does not save_with_current_level
@user.habits.create(committed: committed, date_started: date_started, trigger: trigger, action: action, target: target, reward: reward, order: order, missed_days: missed_days).save_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
habit.rb
class Habit < ActiveRecord::Base
belongs_to :user
has_many :levels, -> { order(:id) }
before_save :current_level
def save_with_current_level
self.levels.build
self.levels.build
self.levels.build
self.levels.build
self.levels.build
self.save
end
def current_habit_level
self.levels.order("id asc").limit(self.current_level).last
end
attr_accessor :missed_one, :missed_two, :missed_three
def completed=(boolean)
self.completed_at = boolean ? Time.current : nil
end
def completed
completed_at && completed_at >= Time.current.beginning_of_day
end
def self.committed_for_today
today_name = Date::ABBR_DAYNAMES[Date.today.wday].downcase
ids = all.select { |h| h.committed.include? today_name }.map(&:id)
where(id: ids)
end
def current_level_strike
levels[current_level - 1] # remember arrays indexes start at 0
end
def current_level
return 0 unless date_started
def committed_wdays
committed.map do |day|
Date::ABBR_DAYNAMES.index(day.titleize)
end
end
def n_days
((date_started.to_date)..Date.yesterday).count do |date|
committed_wdays.include? date.wday
end - self.real_missed_days
end
case n_days
when 0..9
1
when 10..24
2
when 25..44
3
when 45..69
4
when 70..99
5
else
6
end
end
def real_missed_days
value = 0
levels.each do |level|
value += level.missed_days + level.days_lost
end
value
end
def calculate_days_lost
def n_days
((date_started.to_date)..Date.yesterday).count do |date|
committed_wdays.include? date.wday
end - self.real_missed_days
end
case n_days
when 0..9
n_days
when 10..24
n_days-10
when 25..44
n_days-25
when 45..69
n_days-45
when 70..99
n_days-70
else
n_days-100
end
end
def days_left_in_current_level
def n_days
((date_started.to_date)..Date.yesterday).count do |date|
committed_wdays.include? date.wday
end - self.real_missed_days
end
case n_days
when 0..9
10-n_days
when 10..24
25-n_days
when 25..44
45-n_days
when 45..69
70-n_days
when 70..99
100-n_days
else
0 # No end
end
end
end
I'm not exactly sure what your problem is, but I think it's again that you're not saving you're levels. Also you have to save your habits first, otherwise ther is no id to assign your levels to
def save_with_current_level
self.save!
5.times {self.levels.create!}
end
这篇关于如何使用“save_with_current_level"保存会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!