如何保存复选框&为每个复选标记增加整数?

时间:2015-02-25 17:42:32

标签: ruby-on-rails ruby model save integer

如果用户勾选了一个框,我们如何保存它?

现在如果发生这种情况,刷新后复选标记就会消失,在索引<%= habit.missed %>中它仍会显示0而不是1(我不知道是真还是假布尔值适用于此。)

有15个复选框,对于用户检查的每个框,我们如何向t.integer :missed添加+1?

换句话说,如果单击则复选框表示1,如果未单击则表示0表示。

LAYOUT LOOK EX

        Missed:
Level 1: [ ] [ ] [ ]
Level 2: [√] [√] [ ]
Level 3: [√] [ ] [ ]
Level 4: [√] [√] [√] [ ]
Level 5: [ ] [ ] [ ]

结束目标

然后+1会在:level添加1天,因为如果您没有这样做,您就不会计算完一天。 我们如何为每个点击的框添加+1到:level

通过上面的示例,用户需要额外2天才能进入3级,在他可以进入4级之前的一天,等等。

如果您错过了三天的某个级别,我们如何强制用户重新启动该级别?例如,如果我在第66天并且已选中:missed如上所示,在第4级行中有三次我们如何重新计算当天的45天?

此时,对于第4级,需要在每个复选标记之后通过AJAX出现一个额外的框,以防他第二次错过一天。我们可以刷新那个级别所显示的方框,我不会这么想,因为我们仍然需要计算他离开的天数。

&#13;
&#13;
class Habit < ActiveRecord::Base
belongs_to :user
before_save :set_level
acts_as_taggable
serialize :committed, Array

scope :missed, -> { where(missed: 1) }

def levels
		committed_wdays = committed.map { |day| Date::DAYNAMES.index(day.titleize) }
		n_days = ((date_started.to_date)..Date.today).count { |date| committed_wdays.include? date.wday }

  case n_days	  
  when 0..9
    1
  when 10..24
    2
  when 25..44
    3
  when 45..69
    4
  when 70..99
    5
  else
    "Mastery"
	end
end

protected
def set_level
 self.level = levels
end	
end
end
&#13;
&#13;
&#13;

&#13;
&#13;
<%= simple_form_for(@habit) do |f| %>
  <%= f.error_notification %>

<div class="america">
<form>

  <div class="committed">
  <%= f.label "Committed to:" %>&nbsp;
  <%= f.collection_check_boxes :committed, Date::DAYNAMES, :downcase, :to_s %>
  </div>

  <p><div class="date-group">
    <label> Started: </label>
    <%= f.date_select :date_started, :order => [:month, :day, :year], class: 'date-select' %>
  </div></p>

  <p><%= f.text_field :trigger, class: 'form-control', placeholder: 'Enter Trigger' %></p>
  <p><%= f.text_field :tag_list, habit: @habit.tag_list.to_s.titleize, class: 'form-control', placeholder: 'Enter Action' %></p>
  <p><%= f.text_field :target, class: 'form-control', placeholder: 'Enter Target' %></p>
  <p><%= f.text_field :positive, class: 'form-control', placeholder: 'Enter Reward' %></p>
  <%= f.text_field :negative, class: 'form-control', placeholder: 'Enter Penalty' %>
<% 5.times do |n| %>
  <%= f.label "Level #{n+1}:" %>
  <%= check_box_tag 'missed[]', value: n+1 %>
<% end %>

<div class="america2">
  <%= button_tag(type: 'submit', class: "btn") do %>
  <span class="glyphicon glyphicon-plus"></span>
  <% end %>

  <%= link_to habits_path, class: 'btn' do %>
  <span class="glyphicon glyphicon-chevron-left"></span>
  <% end %>

  <%= link_to @habit, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn' do %>
  <span class="glyphicon glyphicon-trash"></span>
  <% end %>
</div>
  
</form>
</div>
<% end %>
&#13;
&#13;
&#13;

&#13;
&#13;
class HabitsController < ApplicationController
  before_action :set_habit, only: [:show, :edit, :update, :destroy]
  before_action :logged_in_user, only: [:create, :destroy]

  def index
    if params[:tag]
      @habits = Habit.tagged_with(params[:tag])
    else
      @habits = Habit.all.order("date_started DESC")
      @habits = current_user.habits
    end
  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
        @feed_items = []
        render 'pages/home'
    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, :date_started, :trigger, :target, :positive, :negative, :tag_list, :committed => [])
    end
end
&#13;
&#13;
&#13;

谢谢=]

P.S。只要完成工作,就可以使用选择框,复选框,collection_box等等。

1 个答案:

答案 0 :(得分:0)

如果您想要一堆复选框,请执行以下操作:

<%- 5.times do |n| %->
  <%= f.label "Level #{n+1}:" %>
  <%= check_box_tag 'missed[]', value: n+1 %>
<%- end -%>

然后你应该会收到一个missed个参数,其中包含一系列数字,每个参数对应于是否检查过那一天。如果您想知道错过了多少天:

[params[:missed]].flatten.length

这处理未定义params[:missed]的情况。