1
votes

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

1

1 Answers

0
votes

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 %>