Rails 7 Web App form not displaying numeric values

Viewed 38

I have a web app that I inherited from another individual and have been slogging through trying to upgrade it to a newer version of Ruby and Rails. I am now on 3.0.3 and Rails 7.0.2. I have a form for entering billable time as below:

form example

All the data in the background is correct and actually goes into the database correctly when creating a new project. The form above is the edit form. It only displays the values for the fields populating with strings. The from/to hours/minutes fields are all integers. They flash on the screen very briefly then disappear. I am using simple_forms to create the form.

.row
  .col-md-3
    = f.input :all_day, :collection => YesNo, :include_blank => false, :input_html => { "ng-model" => "billable_item.all_day", "ng-change" => "fillFields()" }
  .col-md-3  
    = f.input :lunch_break, :collection => LunchBreak, :include_blank => false, :label => "Lunch Break (Optional - mins)", :input_html => { "ng-model" => "billable_item.lunch_break", "ng-change" => "computeTime()" }
.row
  .col-md-3
    = f.input :from_hour, :collection => Hours, :include_blank => false, :input_html => { "ng-model" => "billable_item.from_hour", "ng-change" => "computeTime()" }
  .col-md-3  
    = f.input :from_minute, :collection => Minutes, :include_blank => false, :input_html => { "ng-model" => "billable_item.from_minute", "ng-change" => "computeTime()" }
  .col-md-2
    = f.input :from_unit, :collection => FromToUnits, :include_blank => false, :input_html => { "ng-model" => "billable_item.from_unit", "ng-change" => "computeTime()" }

The above is a piece of code for the form. The collections for Hours and Minutes are:

Hours         = [0,1,2,3,4,5,6,7,8,9,10,11,12]
Minutes       = [0,15,30,45]

if I set the f.input for :from_hour to include :as=>:integer, the value shows up when I load the edit page but the field now has the arrows displayed for incrementing the value instead of the dropdown with the defined collection. The from_unit and all_day fields display properly since they are all text based values for the collection and what is stored in the database.

What am I missing here? this seems like it sure is an issue with typing of the value being read when it goes to display. It affects every single form in my app that has any numeric fields. This worked with no issues using Ruby 2.4.4 and Rails 5.1.2. The code for the controller is also below if that helps:

class BillablesController < ApplicationController
  before_action :initialize_instance_vars_for_drop_downs

  def show
    @billable     = Billable.find(params[:id])
  end

  def new
    gon.start_date_str  = get_lowest_allow_date_string
    @billable           = Billable.new
    @billable.work_date = @this_date
    @billable.from_hour = 0
    @billable.to_hour   = 0
    @billable.from_unit = 'AM'
    @billable.to_unit   = 'AM'
    gon.billable_item   = @billable
  end

  def create
    params[:billable][:from_minute] = 0 if params[:billable][:from_minute].blank?
    params[:billable][:to_minute]   = 0 if params[:billable][:to_minute].blank?
    @billable = Billable.new(billable_params)
    if @billable.save
      flash[:success] = 'Billable successfully created.'
      redirect_to account_daily_path(:selected_date => @this_date)
    else
      flash.now[:error] = @billable.errors[:base][0] if @billable.errors[:base].present?
      render 'new'
    end
  end

  def edit
    gon.start_date_str  = get_lowest_allow_date_string
    @billable           = Billable.find(params[:id])
    gon.billable_item   = @billable
  end

  def update
    @billable = Billable.find(params[:id])
    if @billable.update(billable_params)
      flash[:success] = 'Billable successfully udpated'
      redirect_to account_daily_path(:selected_date => @this_date)
    else
      flash.now[:error] = @billable.errors[:base][0] if @billable.errors[:base].present?
      render 'edit' 
    end
  end

  def destroy
    billable = Billable.find(params[:id])
    billable.destroy
    flash[:success] = 'Billable record successfully deleted.'
    redirect_to account_daily_path(:selected_date => @this_date)
  end

  private

  def initialize_instance_vars_for_drop_downs
    @this_date = Date.new(current_user.calendar_year,current_user.calendar_month,current_user.calendar_day)
    @projects  = Project.where(:active => 'Yes').select("id,name").order(:name)
  end

  def billable_params
    params.require(:billable).permit(:note, :work_date, :from_hour, :from_minute, :from_unit,
                                     :to_hour, :to_minute, :to_unit, :project_id, :user_id,
                                     :computed_hours, :computed_minutes, :status, :all_day,
                                     :lunch_break, :tools_attributes, :billable_total_id,
                                     :tools_attributes => [:id, :computed_hours, :computed_minutes,
                                     :software_id, :property_count, :status, :billable_id, :_destroy])
  end
end
0 Answers
Related