[JAVA] [For beginners] I want to automatically enter pre-registered data in the input form with a selection command.

I was in trouble

――I wanted to set the pre-registered data in the pull-down and select it to automatically fill in the form. ――I tried various articles, but there were many methods that did not work when using the form helper. --There was a way to stop the form helper, but I thought it would be desirable to use the form helper for security.

Thing you want to do

--Register the template data in advance --When selected with the selection command, it is automatically filled in the input form. ――I want to be able to change the numerical value at the time of registration, not just a template

Development environment

ruby '2.6.6'
gem 'rails', '~> 6.0.3', '>='

Implementation details

--Data is registered in advance in the undone_action model (columns are action_name and default_time). --Set undone_actions in the select box --When you select the select box, an event occurs and the saves_name and earned_time of the input form are filled in.


<%= form_with(model: @savings_record, local: true) do |f| %>
  <div class="form-group>
    <label>My unnecessary behavior</label>
    <select multiple id="my_undone_action" class="form-control" onChange='auto_input()'>
      <option disabled selected value>Choose...</option>
      <% current_user.undone_actions.each do |undone_action| %>
        <option value= "<%= undone_action.action_name %>&<%= undone_action.default_time %>"><%= undone_action.action_name %></option>
      <% end %>

  <div class="form-group">
    <%= f.label :savings_name, "Unnecessary behavior" %>
    <%= f.text_field :savings_name, autofocus: true %>

  <div class="form-group">
    <%= f.label :earned_time, "Time required(Minutes)" %>
    <%= f.number_field :earned_time, autofocus: true %>

  <div class="actions">
    <%= f.submit "save money" %>
<% end %>

  var auto_input = function () {
    var element = document.getElementById("my_undone_action");
    document.getElementById("savings_record_savings_name").value = element.value.split("&")[0];
    document.getElementById("savings_record_earned_time").value = element.value.split("&")[1];

Completed form

Select command, before clicking

スクリーンショット 2020-11-03 23.40.07.png

Select command, after clicking

スクリーンショット 2020-11-03 23.43.04.png


I posted the points that I stumbled upon while creating a portfolio with the aim of changing jobs to a web-based company. I hope it helps someone who has the same problems.

Recommended Posts

[For beginners] I want to automatically enter pre-registered data in the input form with a selection command.
[Rails] I want to send data of different models in a form
I want to create a form to select the [Rails] category
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to distinct the duplicated data with has_many through
I want to return multiple return values for the input argument
I want to pass the startup command to postgres with docker-compose.
Implemented a strong API for "I want to display ~~ on the screen" with simple CQRS
I want to return a type different from the input element with Java8 StreamAPI reduce ()
I want to create a chat screen for the Swift chat app!
[Java] I want to perform distinct with the key in the object
[For super beginners] The minimum knowledge you want to keep in mind with hashes and symbols
I want to select multiple items with a custom layout in Dialog
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
I want to display a PDF in Chinese (Korean) with thin reports
I want to ForEach an array with a Lambda expression in Java
I want to import the pull-down menu items when submitting a form in Rails into CSV and display them from the DB data.
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Javassist second decoction)
[Go To Travel] I searched for a plan with a quo card in Jalan
How to store data simultaneously in a model associated with a nested form (Rails 6.0.0)
I tried to automatically generate a class to convert from a data class to a Bundle with APT
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (black magic edition)
I want to get the value in Ruby
Even if I want to convert the contents of a data object to JSON in Java, there is a circular reference ...
I want to change the path after new registration after logging in with multiple devises.
[Rails] I want to display the link destination of link_to in a separate tab
I want to embed any TraceId in the log
I want to use a little icon in Rails
I want to dark mode with the SWT app
I want to monitor a specific file with WatchService
I want to define a function in Rails Console
I want to click a GoogleMap pin in RSpec
I want to create a generic annotation for a type
I want to add a delete function to the comment function
After posting an article with Rails Simple Calendar, I want to reflect it in the calendar.
I want to control the maximum file size in file upload for each URL in Spring Boot
I want to recreate the contents of assets from scratch in the environment built with capistrano
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
Tutorial to create a blog with Rails for beginners Part 1
I want to find a relative path in a situation using Path
I want to set the conditions to be displayed in collection_check_boxes
I want to make a list with kotlin and java!
I want to call a method and count the number
I want to make a function with kotlin and java!
I searched for a web framework with Gem in Ruby
I want to give a class name to the select attribute
I want to create a Parquet file even in Ruby
Tutorial to create a blog with Rails for beginners Part 2
I want to transition to the same screen in the saved state
Tutorial to create a blog with Rails for beginners Part 0
[Java] I want to test standard input & standard output with JUnit
I want to simplify the conditional if-else statement in Java
A memorandum when you want to see the data acquired by Jena & SPARQL for each variable.
[Java] How to search for a value in an array (or list) with the contains method
If you want to mock a method in RSpec, you should use the allow method for mock and the singleton method.
How to deal with the type that I thought about writing a Java program for 2 years
I tried to create an API to get data from a spreadsheet in Ruby (with service account)
I want to recursively search for files under a specific directory
I tried to solve the problem of "multi-stage selection" with Ruby
I want to add a browsing function with ruby on rails
I tried to sort the data in descending order, ascending order / Rails