用户如何选择星期几?

时间:2015-02-09 03:18:47

标签: ruby-on-rails date

如何为一周中的每一天创建一个复选框,使其类似于:

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

然后,用户将在_form中检查他承诺进行30天习惯挑战的日期。

EX:

[]太阳[✓]星期一[✓]星期二[✓]星期三[✓]星期四[✓]星期五[]星期六

我不认为下面的字符串或类似内容会起作用:

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天习惯挑战中剩下的天数很多天他都致力于/错过了。

但回到这个具体问题:

如何为一周中的每一天创建复选框?此功能适用于: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

提前感谢您的帮助!

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

1 个答案:

答案 0 :(得分:0)

我认为这是最好的方法:

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