如何整合:错过的日子:养成习惯的日子.rb? [英] How to integrate :missed days with :committed days in habits.rb?

查看:194
本文介绍了如何整合:错过的日子:养成习惯的日子.rb?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何整合 t.integer:错过 t.text:已提交以便


  1. 当用户检查他:错过 3 :提交 中的天数:他必须重新启动:level

  1. when a User checks off he :missed 3 :committed days in a :level he has to restart the :level?

每个:错过了那天他还会关闭,额外的:已提交 day被添加回:level ,以便他必须在推进之前补足?

for each :missed day he checks off, an additional :committed day is added back into the :level so that he must make it up before advancing?

每个习惯在掌握之前有5个等级!

Each habit has 5 levels before "Mastery" is achieved!

class Habit < ActiveRecord::Base
	belongs_to :user
	before_save :set_level
	acts_as_taggable
	serialize :committed, Array

  def self.comitted_for_today
    today_name = Date::DAYNAMES[Date.today.wday].downcase
    ids = all.select { |h| h.committed.include? today_name }.map(&:id)
    where(id: ids)
  end

	def levels
			committed_wdays = committed.map { |day| Date::DAYNAMES.index(day.titleize) }
			n_days = ((date_started.to_date)..Date.today).count { |date| committed_wdays.include? date.wday }

  case n_days	  
	  when 0..9
	    1
	  when 10..24
	    2
	  when 25..44
	    3
	  when 45..69
	    4
	  when 70..99
	    5
	  else
	    "Mastery"
		end
	end

private
	def set_level
	 self.level = levels
	end	
end

我猜我们必须区分:错过:错过这里取决于它所指的等级。

I'm guessing we would have to distinguish :missed from :missed here depending on what level it is referring to.

habit / _form.html.erb

  <label> Missed: </label>
  <div>
  <label> Level 1: </label>
  <%= f.check_box :missed %>
  <%= f.check_box :missed %>
  <%= f.check_box :missed %>
  </div>
  <div>
  <label> Level 2: </label>
  <%= f.check_box :missed %>
  <%= f.check_box :missed %>
  <%= f.check_box :missed %>
  </div>
  <div>
  <label> Level 3: </label>
  <%= f.check_box :missed %>
  <%= f.check_box :missed %>
  <%= f.check_box :missed %>
  </div>
  <div>
  <label> Level 4: </label>
  <%= f.check_box :missed %>
  <%= f.check_box :missed %>
  <%= f.check_box :missed %>
  </div>
  <div>
  <label> Level 5: </label>
  <%= f.check_box :missed %>
  <%= f.check_box :missed %>
  <%= f.check_box :missed %>
  </div>

habit_controller.rb

class HabitsController < ApplicationController
  before_action :set_habit, only: [:show, :edit, :update, :destroy]
  before_action :logged_in_user, only: [:create, :destroy]

  def index
    if params[:tag]
      @habits = Habit.tagged_with(params[:tag])
    else
      @habits = Habit.all.order("date_started DESC")
      @habits = current_user.habits
    end
  end
                                              
private
                                              
    def habit_params
      params.require(:habit).permit(:missed, :left, :level, :date_started, :trigger, :target, :positive, :negative, :tag_list, :committed => [])
    end
end

_create_habits.rb

class CreateHabits < ActiveRecord::Migration
  def change
    create_table :habits do |t|
      t.integer :missed
      t.integer :level
      t.text :committed
      t.datetime :date_started
      t.string :trigger
      t.string :target
      t.string :positive
      t.string :negative
      t.references :user, index: true

      t.timestamps null: false
    end
    add_foreign_key :habits, :users
    add_index :habits, [:user_id, :created_at]
  end
end

:已提交完美无缺,但现在:错过了无用。请帮我添加适当的逻辑来集成:错过:已提交

:committed works perfectly, but right now :missed serves no purpose. Please help me add the appropriate logic to integrate :missed with :committed.

非常感谢你的时间!

@ Dimitry_N的回答并没有达到这个问题的1)或2),就像我试图让它发挥作用一样。也许你会更好地融入他的逻辑。有了他的回答我也得到了这个错误:如何解决level.rb使用:承诺日期?

@Dimitry_N's answer doesn't achieve either 1) or 2) of this question as much as I've tried to make it work. Maybe you'll have a better luck incorporating his logic. With his answer I also get this error: How to fix level.rb to work with :committed days?

推荐答案

你应该打破问题,因为这很多在一个问题中提出要求。 Dimitry_N似乎在正确的轨道上,但你现在需要将一些逻辑添加到关卡模型中。如果您想了解详细信息,请与我聊聊。

You should break the question done because this is a lot to ask for in one question. Dimitry_N seemed to be on the right track, but you'll need to add some of your logic to the levels model now. Please chat with me if you want to go over the details.

这篇关于如何整合:错过的日子:养成习惯的日子.rb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆