如何从主页设置:completed_at
时间戳?
[2] pry(main)> Habit.find(18)
id: 1,
missed_days: 0,
conceal: false,
committed: ["tue", "wed", "thu", "fri", ""],
date_started: Wed, 22 Jul 2015 00:00:00 EDT -04:00,
action: "test",
user_id: 1,
created_at: Wed, 22 Jul 2015 17:37:59 EDT -04:00,
updated_at: Wed, 22 Jul 2015 17:38:01 EDT -04:00,
completed_at: nil,
控制器
def home
@habits = current_user.habits.committed_for_today.incomplete.order(:order)
end
通过设置:completed_at
模型将让控制器知道习惯已经完成(又名incomplete
)
habit.rb
class Habit < ActiveRecord::Base
belongs_to :user
has_many :levels, -> { order(:id) }
serialize :committed, Array
before_save :current_level
attr_accessor :missed_one, :missed_two, :missed_three
scope :incomplete, -> { where(completed_at: nil) }
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 save_with_current_level
self.levels.build
self.levels.build
self.levels.build
self.levels.build
self.levels.build
self.save
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
我想用:completed_at
或link_to
触发button_tag
。然后,一旦点击其中一个内容并设置了:completed_at
,主页就会刷新redirect_to root_url
,现在已删除:completed_at
习惯,因为主页仅显示incomplete
个人的习惯
以下是 Gist 。
答案 0 :(得分:1)
我更喜欢这种 Ajax 方式。
在你看来
#home view page
<table>
<% @habits.each do |habit| %>
<tr>
<td><%= link_to "Mark Completed", mark_completed_path(habit), remote: true, method: 'put', class: 'update_habit' %></td>
</tr>
<% end %>
</table>
<script>
$('.update_habbit').bind('ajax:success', function() {
$(this).closest('tr').fadeOut();
});
</script>
如果要删除,请将script
更改为
<script>
$('.update_habbit').bind('ajax:success', function() {
$(this).closest('tr').remove();
});
</script>
制作路线
#routes.rb
put '/mark_completed/:id', to: 'habits#mark_completed', as: 'mark_completed'
在HabbitsController
制作方法
def mark_completed
@habit = Habit.find(params[:id])
@habit.update(completed_at: Time.now)
respond_to do |format|
format.html
format.js { render :nothing => true }
end
end
答案 1 :(得分:0)
您可以创建HabitsController#complete
操作,并使用表单发送请求。由于您没有分享您的观点,我假设您有一个habit
变量,其中包含您要完成的对象。
<%= button_to 'Complete', complete_habit_path(habit) %>
动作的代码:
def complete
habit = Habit.find(params[:id])
habit.complete = true
habit.save
# or habit.update(completed_at: Time.current)
redirect_to root_url
end