14
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Rails】form_for の number_field で小数を入力できるようにする

Posted at

問題

小数点第1位まで数字を入れたかったのに、デフォルトだと整数しか入れられずエラーを吐かれてしまう。
スクリーンショット 2019-10-11 22.28.49.png

解決策

number_fieldのあとにstep: "0.1"と入れる。

<%= form_for @test do |f| %>

  <%= f.label :subject %>
  <%= f.text :subject %>

  <%= f.label :score %>
  <%= f.number_field :score, step: "0.1" %>

  <%= f.submit "送信" %>
<% end %>

ちなみに、step: "100"とすると矢印を押したときのカウントが100刻みになったりします。

14
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?