money-rails gem不允许我将美分输入文本框

时间:2016-10-21 20:24:12

标签: 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_model_currency: :price_currency
    monetize :new_price_cents, with_model_currency: :price_currency

end

_form.html.erb:

<%= 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 %>
  </div>
  <div class="field">
    <%= f.label :cib_price %><br>
    <%= f.number_field :cib_price %>
  </div>
  <div class="field">
    <%= f.label :new_price %><br>
    <%= f.number_field :new_price %>
  </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

我已经进入rails控制台并手动更改了值,但它似乎是基于数据库值的错误(db值= 2011美分,错误=&#34;请输入有效值。最近的两个值为20.11和21.11。&#34;)。

任何建议都将不胜感激!

编辑: 我想到了。通过添加&#34;步骤:0.01&#34;在number_field方法的末尾,它允许您将值增加.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 %>

0 个答案:

没有答案