我试着把养成好习惯的游戏化。
当用户创造了一个新的习惯,在达到“掌握”之前有5个等级!每个级别都有一定数量的与其相关的天数(如habits.rb n_days所示)。
这就是我被困的地方:尽管在missed_days _form中检查:level 1的:level框,但控制台显示了missed_days: 0 for Habit.find(1),但missed_days: 1 for Level.find(1)。
错过一天是件坏事,因为如果您错过了一天(通过复选框表示),就会将一天添加回您的current_level中(通过days_missed控制器),换句话说,只有完成的天数才能完成一个级别。
我们如何才能得到它,使在:missed_days中有多少:level也是:habit中相同的整数
习惯has_many水平。

habits _form
<label id="<%= @habit.id %>" class="habit-id"> Missed: </label>
<% @habit.levels.each_with_index do |level, index| %>
<p>
<label id="<%= level.id %>" class="level-id"> Level <%= index + 1 %>: </label>
<%= check_box_tag nil, true, level.missed_days > 0, {class: "habit-check"} %>
<%= check_box_tag nil, true, level.missed_days > 1, {class: "habit-check"} %>
<%= check_box_tag nil, true, level.missed_days > 2, {class: "habit-check"} %>
</p>
<% end %>
<% end %>habit-check是关于habit.js的
$(document).ready(function()
{
$(".habit-check").change(function()
{
habit = $(this).parent().siblings(".habit-id").first().attr("id");
level = $(this).siblings(".level-id").first().attr("id");
if($(this).is(":checked"))
{
$.ajax(
{
url: "/habits/" + habit + "/levels/" + level + "/days_missed",
method: "POST"
});
}
else
{
$.ajax(
{
url: "/habits/" + habit + "/levels/" + level + "/days_missed/1",
method: "DELETE"
});
}
});
});当我单击复选框时,这就是在终端中发生的情况:
Started POST "/habits/2/levels/6/days_missed" for 127.0.0.1 at 2015-04-28 13:57:20 -0400
Processing by DaysMissedController#create as */*
Parameters: {"habit_id"=>"2", "level_id"=>"6"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Habit Load (0.2ms) SELECT "habits".* FROM "habits" WHERE "habits"."user_id" = ? [["user_id", 1]]
Habit Load (0.4ms) SELECT "habits".* FROM "habits"
ActsAsTaggableOn::Tag Load (0.2ms) SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('ingrain'))
Habit Load (0.2ms) SELECT "habits".* FROM "habits" WHERE "habits"."id" = ? LIMIT 1 [["id", 2]]
Level Load (0.1ms) SELECT "levels".* FROM "levels" WHERE "levels"."habit_id" = ? AND "levels"."id" = ? LIMIT 1 [["habit_id", 2], ["id", 6]]
(0.1ms) begin transaction
SQL (0.3ms) UPDATE "levels" SET "missed_days" = ?, "updated_at" = ? WHERE "levels"."id" = ? [["missed_days", 1], ["updated_at", "2015-04-28 17:57:20.960578"], ["id", 6]]
(3.0ms) commit transaction
Completed 200 OK in 27ms (ActiveRecord: 4.7ms)
habits.rb
class Habit < ActiveRecord::Base
belongs_to :user
has_many :comments, as: :commentable
has_many :levels
serialize :committed, Array
validates :date_started, presence: true
before_save :current_level
acts_as_taggable
scope :private_submit, -> { where(private_submit: true) }
scope :public_submit, -> { where(private_submit: false) }
attr_accessor :missed_one, :missed_two, :missed_three
def save_with_current_level
self.levels.build
self.levels.build
self.levels.build
self.levels.build
self.levels.build
self.save
end
def self.committed_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 current_level
return 0 unless date_started
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 }
actual_days = n_days - self.missed_days
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
enddays_missed_controller.rb
class DaysMissedController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
def create
level = Habit.find(params[:habit_id]).levels.find(params[:level_id])
level.missed_days = level.missed_days + 1
level.save!
head :ok # this returns an empty response with a 200 success status code
end
def destroy
level = Habit.find(params[:habit_id]).levels.find(params[:level_id])
level.missed_days = level.missed_days - 1
level.save!
head :ok # this returns an empty response with a 200 success status code
end
endroutes.rb
resources :habits do
resources :comments
resources :levels do
# we'll use this route to increment and decrement the missed days
resources :days_missed, only: [:create, :destroy]
end
end下面是它的要点:https://gist.github.com/RallyWithGalli/c66dee6dfb9ab5d338c2
发布于 2015-05-01 04:39:27
改变模型和控制器。
habits.rb模型
def current_level
return 0 unless date_started
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 } - self.missed_days #Added this line here.我安装了控制器,这就是我将missed_days级别提高到相同习惯missed_days的位置。
class DaysMissedController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
def create
habit = Habit.find(params[:habit_id])
habit.missed_days = habit.missed_days + 1
habit.save!
level = habit.levels.find(params[:level_id])
level.missed_days = level.missed_days + 1
level.save!
head :ok # this returns an empty response with a 200 success status code
end
def destroy
habit = Habit.find(params[:habit_id])
habit.missed_days = habit.missed_days - 1
habit.save
level = habit.levels.find(params[:level_id])
level.missed_days = level.missed_days - 1
level.save!
head :ok # this returns an empty response with a 200 success status code
end
end发布于 2015-04-30 05:25:02
你错过的日子可能没有出现,因为你的复选框实际上没有名字或id。他们用“零”代替..。尝试:
<%= check_box_tag "missed_days_1", true, level.missed_days > 0, {class: "habit-check"} %> 或者类似的..。第二和第三是2和3.记住,一个复选框不会给你missed_days => 1 --它会给你:missed_days_1 => true如果你真的想要:missed_days =>1,那么你需要单选按钮.但是你仍然需要给他们一个:missed_days的名字
“有没有办法在days_missed控制器中单击复选框=1?”
也许-您可以设置复选框选中时返回的值.标签多佐在第二个param中传递它(而不是true)。但问题是,您有多个复选框,如果您的复选框命名为相同的东西,它们将相互覆盖,只有最后一组的值才会被返回。
试着给它们命名如下:看看结果是什么,你会得到什么。然后编写代码,您需要破译这意味着什么,然后将正确的值放在正确的位置。
<% @habit.levels.each_with_index do |level, index| %>
<%= check_box_tag "missed_days[#{level}][1]", true, level.missed_days > 0, {class: "habit-check"} %>
<%= check_box_tag "missed_days[#{level}][2]", true, level.missed_days > 0, {class: "habit-check"} %>
<%= check_box_tag "missed_days[#{level}][3]", true, level.missed_days > 0, {class: "habit-check"} %>
<% end %>考虑到rails的默认值..。第一种可能仍然会变成“真”.(您需要自己检查返回值,以确定是否是这样)
https://stackoverflow.com/questions/29885577
复制相似问题