Ruby on rails money rails gem不允许我在文本框中输入美分
我在使用money rails gem将值保存到数据库时遇到问题。每当我尝试在文本框(即20.22)中输入一个带美分的值时,就会出现一个错误,提示“请输入一个有效值。两个最接近的值是20和21。” 型号:Ruby on rails money rails gem不允许我在文本框中输入美分,ruby-on-rails,money-rails,Ruby On Rails,Money Rails,我在使用money rails gem将值保存到数据库时遇到问题。每当我尝试在文本框(即20.22)中输入一个带美分的值时,就会出现一个错误,提示“请输入一个有效值。两个最接近的值是20和21。” 型号: class VideoGame < ActiveRecord::Base monetize :loose_price_cents, with_model_currency: :price_currency monetize :cib_price_cents, with_
class VideoGame < ActiveRecord::Base
monetize :loose_price_cents, with_model_currency: :price_currency
monetize :cib_price_cents, with_model_currency: :price_currency
monetize :new_price_cents, with_model_currency: :price_currency
end
我已经进入rails控制台并手动更改了周围的值,但它似乎以数据库值(db value=2011美分,error=“请输入一个有效值。两个最接近的值是20.11和21.11。”)作为错误的基础
任何建议都将不胜感激
编辑:
我想出来了。通过将“步骤:0.01”添加到数字_字段方法的末尾,您可以将值增加0.01,而不是默认情况下的1。它应该是这样的:
<%= form_for(@video_game) do |f| %>
<% if @video_game.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@video_game.errors.count, "error") %> prohibited this video_game from being saved:</h2>
<ul>
<% @video_game.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :game %><br>
<%= f.text_field :game %>
</div>
<div class="field">
<%= f.label :loose_price %><br>
<%= f.number_field :loose_price, step: 0.01 %>
</div>
<div class="field">
<%= f.label :cib_price %><br>
<%= f.number_field :cib_price, step: 0.01 %>
</div>
<div class="field">
<%= f.label :new_price %><br>
<%= f.number_field :new_price, step: 0.01 %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
禁止保存此视频游戏:
def update
respond_to do |format|
if @video_game.update(video_game_params)
format.html { redirect_to @video_game, notice: 'Video game was successfully updated.' }
format.json { render :show, status: :ok, location: @video_game }
else
format.html { render :edit }
format.json { render json: @video_game.errors, status: :unprocessable_entity }
end
end
end
<%= form_for(@video_game) do |f| %>
<% if @video_game.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@video_game.errors.count, "error") %> prohibited this video_game from being saved:</h2>
<ul>
<% @video_game.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :game %><br>
<%= f.text_field :game %>
</div>
<div class="field">
<%= f.label :loose_price %><br>
<%= f.number_field :loose_price, step: 0.01 %>
</div>
<div class="field">
<%= f.label :cib_price %><br>
<%= f.number_field :cib_price, step: 0.01 %>
</div>
<div class="field">
<%= f.label :new_price %><br>
<%= f.number_field :new_price, step: 0.01 %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>