用户如何 Select 星期几?
How Can Users Select Days of the Week?
如何为一周中的每一天创建一个复选框,使其成为这样的:
[] 周日[] 周一[] 周二[] 周三[] 周四[] 周五[] 周六
然后用户将在 _form 中勾选他承诺进行 30 天习惯挑战的日期。
前:
[] 周日 [✓] 周一 [✓] 周二 [✓] 周三 [✓] 周四 [✓] 周五 [] 周六
我认为字符串或类似下面的内容不起作用:
EX 1
<li>
<input type="checkbox" id="mon"/>
<label for="mon">MON</label>
</li>
EX 2
<%= f.select_tag(:day, [['Monday', 'Monday'], ['Tuesday', 'Tuesday], ...]) %>
因为我希望每一天都实际代表日历上的每一天,这样我最终可以根据用户的开始日期计算出他在 30 天习惯挑战中还剩多少天以及他承诺了多少天to/missed。
但回到这个具体问题:
如何为一周中的每一天创建复选框?此函数适用于 :days
.
<%= simple_form_for(@habit) do |f| %>
<%= f.error_notification %>
<form>
<div class="missed">
<% Habit::MISSED.each do |c| %>
<%= f.radio_button(:missed, c) %>
<%= label(c, c, c) %>
<% end %>
</div>
<br>
<div class="date-group">
<label> Started: </label>
<%= f.date_select :date_started, :order => [:month, :day, :year], class: 'date-select' %>
</div>
<%= f.input :days %>
<%= f.input :trigger %>
<%= f.input :action %>
<%= f.input :target %>
<%= f.input :positive %>
<%= f.input :negative %>
class HabitsController < ApplicationController
before_action :set_habit, only: [:show, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@habits = Habit.all
end
def show
end
def new
@habit = current_user.habits.build
end
def edit
end
def create
@habit = current_user.habits.build(habit_params)
if @habit.save
redirect_to @habit, notice: 'Habit was successfully created.'
else
render action: 'new'
end
end
def update
if @habit.update(habit_params)
redirect_to @habit, notice: 'Habit was successfully updated.'
else
render action: 'edit'
end
end
def destroy
@habit.destroy
redirect_to habits_url
end
private
def set_habit
@habit = Habit.find(params[:id])
end
def correct_user
@habit = current_user.habits.find_by(id: params[:id])
redirect_to habits_path, notice: "Not authorized to edit this habit" if @habit.nil?
end
def habit_params
params.require(:habit).permit(:missed, :left, :level, :days, :date_started, :trigger, :action, :target, :positive, :negative)
end
end
在此先感谢您的帮助!
我认为这是最好的方法:
<%= f.label "Committed to:" %>
<% Date::DAYNAMES.each do |day| %>
<%= f.check_box :days, {}, day %>
<%= day %>
<% end %>
如何为一周中的每一天创建一个复选框,使其成为这样的:
[] 周日[] 周一[] 周二[] 周三[] 周四[] 周五[] 周六
然后用户将在 _form 中勾选他承诺进行 30 天习惯挑战的日期。
前:
[] 周日 [✓] 周一 [✓] 周二 [✓] 周三 [✓] 周四 [✓] 周五 [] 周六
我认为字符串或类似下面的内容不起作用:
EX 1
<li>
<input type="checkbox" id="mon"/>
<label for="mon">MON</label>
</li>
EX 2
<%= f.select_tag(:day, [['Monday', 'Monday'], ['Tuesday', 'Tuesday], ...]) %>
因为我希望每一天都实际代表日历上的每一天,这样我最终可以根据用户的开始日期计算出他在 30 天习惯挑战中还剩多少天以及他承诺了多少天to/missed。
但回到这个具体问题:
如何为一周中的每一天创建复选框?此函数适用于 :days
.
<%= simple_form_for(@habit) do |f| %>
<%= f.error_notification %>
<form>
<div class="missed">
<% Habit::MISSED.each do |c| %>
<%= f.radio_button(:missed, c) %>
<%= label(c, c, c) %>
<% end %>
</div>
<br>
<div class="date-group">
<label> Started: </label>
<%= f.date_select :date_started, :order => [:month, :day, :year], class: 'date-select' %>
</div>
<%= f.input :days %>
<%= f.input :trigger %>
<%= f.input :action %>
<%= f.input :target %>
<%= f.input :positive %>
<%= f.input :negative %>
class HabitsController < ApplicationController
before_action :set_habit, only: [:show, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@habits = Habit.all
end
def show
end
def new
@habit = current_user.habits.build
end
def edit
end
def create
@habit = current_user.habits.build(habit_params)
if @habit.save
redirect_to @habit, notice: 'Habit was successfully created.'
else
render action: 'new'
end
end
def update
if @habit.update(habit_params)
redirect_to @habit, notice: 'Habit was successfully updated.'
else
render action: 'edit'
end
end
def destroy
@habit.destroy
redirect_to habits_url
end
private
def set_habit
@habit = Habit.find(params[:id])
end
def correct_user
@habit = current_user.habits.find_by(id: params[:id])
redirect_to habits_path, notice: "Not authorized to edit this habit" if @habit.nil?
end
def habit_params
params.require(:habit).permit(:missed, :left, :level, :days, :date_started, :trigger, :action, :target, :positive, :negative)
end
end
在此先感谢您的帮助!
我认为这是最好的方法:
<%= f.label "Committed to:" %>
<% Date::DAYNAMES.each do |day| %>
<%= f.check_box :days, {}, day %>
<%= day %>
<% end %>