用户如何选择星期几? [英] How Can Users Select Days of the Week?

查看:97
本文介绍了用户如何选择星期几?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在一周的每一天创建一个复选框,这样就可以这样:



[] Sun [] Mon [] Tue [] Wed [] Thu []周五[]周六



然后,用户将在_form中检查他承诺为做30天的习惯挑战的那一天。



EX:



[] Sun [✓] Mon [✓] Tue [✓] Wed [✓] Thu [✓] Fri [] Sat



我不认为一个字符串或下面的东西可以工作:



  EX 1< li> < input type =checkboxid =mon/> < label for =mon> MON< / label> < / li> EX 2<%= f.select_tag(:day,[['Monday','Monday'],['Tuesday','Tuesday],...])%>  



因为我希望每天在日历上实际表示每一天,最终计算出,根据他的开始日期和他承诺/错过的天数,我如何计算用户在30天的习惯挑战中剩下多少天。



但是回到这个具体问题:



如何创建一周中的每一天的复选框?此功能适用于:days



 <%= simple_form_for )do | f | %> <%= f.error_notification%> < form> < div class =missed> <%Habit :: MISSED.each do | c | %>& nbsp;& nbsp; <%= f.radio_button(:missed,c)%>& nbsp; <%= label(c,c,c)%> <%end%> < / div> < br> < div class =date-group> < label>开始:< / 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%>  



div class =snippetdata-lang =jsdata-hide =false>

  class HabitsController< applicationController before_action:set_habit,only:[:show,:edit,,update,,destroy] before_action:correct_user只有:[: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: '习惯成功地创建了'else render动作:'new'end end def update如果@ habit.update(habit_params)redirect_to @habit,请注意:'Habit已成功更新'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授权编辑这个习惯如果@habit。零? end def habit_params params.require(:habit).permit(:missed,:left,:level,:days,:date_started,:trigger,,action,:target,,positive,:negative)endend  



提前感谢您的帮助!



Github https://github.com/RallyWithGalli/ruletoday

解决方案

我认为这是最好的方法:



 <%= f.label承诺:%>& nbsp;<%Date :: DAYNAMES.each do | day | %> <%= f.check_box:days,{},day%> <%= day%><%end%>  


How can I create a checkbox for each day of the week such that it would be something like this:

[] Sun [] Mon [] Tue [] Wed [] Thu [] Fri [] Sat

The User would then check off in the _form the day(s) he is committed to for doing his 30 day habit challenge.

EX:

[] Sun [✓] Mon [✓] Tue [✓] Wed [✓] Thu [✓] Fri [] Sat

I don't think a string or something like below would work:

EX 1
                <li>
                	<input type="checkbox" id="mon"/>
	                <label for="mon">MON</label>
                </li>

EX 2

                <%= f.select_tag(:day, [['Monday', 'Monday'], ['Tuesday', 'Tuesday], ...]) %>

Because I want each day to actually represent each day on the calendar so that I can eventually figure out how I can calculate for the User how many days he has left in his 30 day habit challenge, based on his start date and how many days he committed to/missed.

But getting back to this specific question:

How do I create the checkboxes for each day of the week? This function would apply to :days.

<%= simple_form_for(@habit) do |f| %>
  <%= f.error_notification %>

  <form>
      <div class="missed">
    <% Habit::MISSED.each do |c| %>&nbsp;&nbsp;
      <%= f.radio_button(:missed, c) %>&nbsp;
      <%= 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

Thanks in advance for your help!

Github: https://github.com/RallyWithGalli/ruletoday

解决方案

I think this is the best way to do it:

<%= f.label "Committed to:" %>&nbsp;
<% Date::DAYNAMES.each do |day| %>
  <%= f.check_box :days, {}, day %>
  <%= day %>
<% end %>

这篇关于用户如何选择星期几?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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