Tips for passing a timestamp to a Postgresql DB using a Button in a Form instead of a traditional rails time_select box

I am currently developing a CAD App in Rails 4 with Ruby 2. The goal is to provide users with a button on the screen to log an on-scene time and clear scene time, especially since most users will be using touch screen computers. Instead of using the default Rails time_select input boxes, I wanted to explore different options.

I'm curious if it's possible to implement this feature, and if so, I would appreciate some ideas and guidance on how to achieve it.

Additionally, I have another question regarding displaying these time boxes only if unit_2 - unit_4 have any content in their respective DB Columns.

This is what my form looks like:

<%= form_for(@call) do |f| %>
    <div class="panel panel-success" id="responding-box">
          <div class="panel-heading"><center><h4>Units Responding</h4></center></div>
          <table class="table">
            <thead>
              <tr>
                <th><center>Primary Responder</center></th>
                <th><center>Time On Scene</center></th>
                <th><center>Time Clear</center></th>
              </tr>
            </thead>

            <tbody>
              <tr>
                <td><center><%= f.collection_select(:user_id, User.all, :id, :employee_ident, {}, { :multiple => false } ) %></center></td>
                <td><center><%= f.time_select :unit_on_scene, {:include_blank => true, :default => nil} %></center></td>
                <td><center><%= f.time_select :unit_clear, {:include_blank => true, :default => nil} %></center></td>
              </tr>
            </tbody>
          </table>
          <br>
          <!-- Code for other units goes here -->
        </div>
      </div>

This image shows the section of the form that needs enhancement: https://i.stack.imgur.com/aSUG0.png

Each unit has two time select boxes, which I aim to turn into buttons that record the time and update the respective table columns for display on the show.html.erb page. Additionally, I want to hide these buttons if the unit_2,3,4 fields are empty until data is entered.

If you can assist with either or both of these tasks, I would greatly appreciate it! Thank you in advance.

EDIT: I have added the following script to my Rails-created form. However, clicking the button redirects me to the show.html.erb page without updating the table:

    <script>
    $('#unit_on_scene').on("click", function() {
    $('#unit_on_scene').val(Date());
    alert('unit_on_scene set to ' + $('#unit_on_scene').val());
    return false;
    })
    </script>

    <form action="" method="POST">
        <input type="hidden" id="unit_on_scene"></input>
        <button class="btn btn-nav btn-primary" id="unit_on_scene" type="submit">On Scene</button>
    </form>
    </center></td>

Answer №1

Question 1 is easily achievable using JavaScript. One way to do this is by creating a button linked to a JavaScript event that will update a hidden input field with the current time.

Check out this Example using jQuery: https://jsfiddle.net/k6emjoLt/

<script type="text/javascript">
  $('#timeBtn').on("click", function(){
     $('#timeField').val(Date());
  })
</script>

Question 2 is also quite simple. You can create a variable in your controller that indicates whether certain fields have been filled out or not.

Here's an Example (make sure to set @unit2 in your controller):

<% if @unit2.present? %>
  <tr>
    <td><center><%= f.text_field :unit_2 %></center></td>
    <td><center><%= f.time_select :unit2_os, {:include_blank => true, :default => nil} %></center></td>
    <td><center><%= f.time_select :unit2_cl, {:include_blank => true, :default => nil} %></center></td>
  </tr>
<% end %>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When utilizing Ionic Firebase, the user profile saved in the sidemenu is stored using the Events class. However, the saved profile disappears as soon as

Hello there. I am currently working on displaying my user's information in the sidemenu, and after some research, I found that using the Events class might solve this issue. However, I have noticed that the data saved in subscribe gets destroyed whene ...

When attempting to change a Component's name from a string to its Component type in Angular 9, an error is thrown stating that the passed-in type is

When working with Template HTML: <ng-container *ngComponentOutlet="getComponent(item.component); injector: dynamicComponentInjector"> </ng-container> In the .ts file (THIS WORKS) getComponent(component){ return component; //compo ...

Resetting the width of a CSS-styled div to its default maximum width

I have a set of automatically generated div containers that contain label and data information. For example: <div class="display-label"> @Html.DisplayNameFor(model => model.BusinessPhone) </div> <div class="display-field"> @H ...

Retrieve pixel information upon touch in an Appcelerator Titanium application on Android or iPhone

Can pixel level data of an image view be accessed using Titanium Mobile on Android/iPhone? ...

Avoiding the use of mailto links in PHP

One problem I encountered is with a mailto link in my PHP code: <a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f5a525e56537f5b50525e5651114b535b">[email protected]</a>?subject=<?php rawurlencode ...

Div with a vertical line

Check out this jsFiddle link Struggling to get a "vertical rule" to span the entire width of its container in a div, specifically adjusting the border-right of a nested div. Margins have been modified, even tried margin-top: 20px; in the #vr, but no luck. ...

I'm having trouble getting the font to display properly on Safari (mac) similar to how it appears on

Recently, I completed a website for a client at . The owner requested the footer text to be styled similarly to the footer text on . However, upon review, it seems that the font in the lists on desiringgod appears thinner compared to the site I built. Thi ...

Web2py exhibits varying behavior between local and online versions, where it executes server code but results in a 404 error

When I run the request on my local version of the application using the code below, it successfully executes on the server. $.ajax({ type: 'POST', url: "{{=URL('default', 'serverFunction.json')}}", data: {id: id} }); Howe ...

Is it possible to create a single CSS style class that can be used for various section tiles on a website?

I am currently working on a website that includes different section titles such as About, Projects, Contact, and more. To make these titles stand out against the background image, I have decided to fill them with white. The text color of the titles is dar ...

Is there a way to retrieve the initial value from the second element within the JSON data?

Exploring JSON Data Collections In my JSON data, I have two collections: FailedCount and SucceededCount. { "FailedCount": [{ "FailedCount_DATE_CURRENT_CHECK": "2016-11-30 10:40:09.0", "FailedCount_DATE__CURRENT__CHECK": "10:40:09", "FailedCo ...

How can I detect a DOM element mutation based on a CSS selector, and if this is possible, how can it be accomplished?

Imagine there's a website with a specific HTML element. It seems that this element has the same class during the DOMContentLoaded event as it does during the load event. However, after the load event, this class (and possibly the ID and other HTML att ...

Facing difficulties in Angular 8 while trying to import firestore and firebase for an authentication system

While attempting to implement Firestore/Firebase functionalities for Google OAuth signin, I encountered an error indicating that Firebase is not imported: https://i.sstatic.net/oL4rY.png CODE: ERROR in node_modules/@angular/fire/auth/auth.d.ts:4:28 - er ...

A JavaScript or CSS file within an HTML document

I understand this may seem like a silly question. However, out of curiosity, is there a way to upload an HTML file (with a .html extension) as a JavaScript or CSS file (renamed with a .js or .css extension), specifying the type header as either HTML or js ...

Is there a way to identify if a user originated from a Google ad and determine if this is their nth page during the session using JavaScript code?

Is there a way for me to execute specific logic when a user, who arrived at the page via a contextual advertisement, reaches a certain page during their session? How can I make this happen? ...

Positioning a designated div directly above a designated spot on the canvas

I'm grappling with a challenge in my game where the canvas handles most of the animation, but the timer for the game resides outside the canvas in a separate div. I've been struggling to center the timer around the same focal point as the squares ...

Guide to setting up a nested vuetify navigation drawer

I am facing a scenario where I have one fixed navigation drawer with icons and another dynamic navigation drawer that should open and close adjacent to the fixed one without overlapping. The current implementation is causing the dynamic drawer to overlap t ...

Sequelize is unable to retrieve a table from the database

I am configuring Sequelize in order to streamline the manipulation of an MSSQL database. My attempt to define a table called 'Stock' has resulted in unexpected behavior when trying to query it. Below is the code snippet I used for defining the t ...

Exploring the differences between utilizing request.body in building RESTful APIs with Django versus Node.js

As I dive into learning the Django framework, my main aim is to leverage this knowledge in creating a rest api. Although I've looked into using django-rest framework, my current job necessitates a focus on Django specifically. In my journey so far, I ...

Parent passing down React state, but child component fails to update it

When a setState function is passed down from a parent component, I aim to update the state of the parent setter if the enter key is pressed. However, despite setting the state, nothing seems to happen and I am left with an empty array. Below is the snippe ...

In TypeScript, use a Record<string, any> to convert to {name: string}

I have developed a custom react hook to handle API calls: const useFetch: (string) => Record<string, any> | null = (path: string) => { const [data, setData] = useState<Record<string, any> | null>(null); var requestOptions: Requ ...