[Ruby] What to do when you get an error saying Please enter a valid value in Rails datetime_field acquisition

1 minute read

Error occurred

When I was implementing the posting form with Rails, it worked on my PC, but

For some reason, when I tried to use it on a smartphone, there was a part where an error occurred, so I will write it.

ruby:new.html.erb


<%= form_for @ride, method: :post do |f| %>
    <h1> Post a new ride! </h1>
    <label>Title</label>
    <p><%= f.text_field :title %></p>
    <label>Scheduled date</label>
    <br />
    <%= f.datetime_field :sch_datetime %><br />
    <br />
    <input class="btn btn-primary" type="submit" value="Post">
  <% end %>

config/initializers/time_formats.rb


Time::DATE_FORMATS[:datetime] ='%Y year %m month %d day %H hour %M minute'

config/locales/application.rb


class Application <Rails::Application
    config.load_defaults 5.2
    config.i18n.default_locale = :ja
    config.assets.paths << config.root.join("vendor/assets/javascripts")
    config.assets.paths << config.root.join("vendor/assets/stylesheets/")
    config.action_view.embed_authenticity_token_in_remote_forms = true
    config.time_zone ='Tokyo'
end

The first thing I wrote was to use datetime_field like this to get the date and time together.

I confirmed that it works on a PC with this method at all, so I deployed it in this state,

Suddenly I tried to check the operation with a smartphone and tried to post it in the date and time part

** I was unable to post with an error saying “Please enter a valid value”. .. .. **

What is the cause?

I thought it was, but I guess it’s not a code mistake because it is working properly on the PC.

The form when inputting on smartphone and PC is also changing, so there seems to be a problem with datetime_field itself ~

I decided to write it separately for date_field and time_field.

ruby:new.html.erb


<%= form_for @ride, method: :post do |f| %>
    <h1> Post a new ride! </h1>
    <label>Title</label>
    <p><%= f.text_field :title %></p>
    <label>Scheduled date</label>
    <br />
# from here
    <%= f.date_field :sch_datetime %><br />
    <br />
    <%= f.time_field :sch_datetime %><br />
# Change to here
    <br />
    <input class="btn btn-primary" type="submit" value="Post">
  <% end %>

When I checked the operation in this state, it worked without problems!