Ruby on rails Rails-编辑视图-Can';t保存到MySQL:decimal使用数字\u到\u货币帮助器

Ruby on rails Rails-编辑视图-Can';t保存到MySQL:decimal使用数字\u到\u货币帮助器,ruby-on-rails,ruby,view,number-to-currency,Ruby On Rails,Ruby,View,Number To Currency,我有两个数据库列(使用mysql),一个是十进制(10,2),另一个是字符串 迁移看起来像 t.decimal :PRICE, precision: 10, scale: 2 t.string :TOTALTAX before_save :strip_currency def strip_currency self.PRICE = self.PRICE.to_s.gsub(/[$,]/,"").to_d self.TOTALTAX = self.TOTALTAX.to_s.gsub

我有两个数据库列(使用mysql),一个是十进制(10,2),另一个是字符串

迁移看起来像

t.decimal :PRICE, precision: 10, scale: 2
t.string :TOTALTAX
before_save :strip_currency
def strip_currency
   self.PRICE = self.PRICE.to_s.gsub(/[$,]/,"").to_d
   self.TOTALTAX = self.TOTALTAX.to_s.gsub(/[$,]/,"")
end
在a _form.html.erb partial中,我使用数字\u to \u currency helper获得以下内容

<div class="form-group">
    <%= f.label :PRICE %>
    <br>
    <%= f.text_field :PRICE, :value => number_to_currency(f.object.PRICE, :precision => 2), :class=>'form-control' %>
</div>
<div class="form-group">
    <%= f.label :TOTALTAX %>
    <br>
    <%= f.text_field :TOTALTAX, :value => number_to_currency(f.object.TOTALTAX, :precision => 2), :class=>'form-control' %>
</div>

我讨厌这样做,但它是有效的。我仍然对其他解决办法持开放态度

在控制器中,我劫持了params,去掉了所有的$and,签名并将我被黑客攻击的params散列发送到.update方法

def update
  respond_to do |format|
    removecurrency = deals_params
    removecurrency['PRICE'] = removecurrency['PRICE'].to_s.gsub(/[$,]/,"")
    removecurrency['TOTALTAX'] = removecurrency['TOTALTAX'].to_s.gsub(/[$,]/,"")
    removecurrency['SAFECOMP'] = removecurrency['SAFECOMP'].to_s.gsub(/[$,]/,"")
    if @deals.update(removecurrency)
      format.html { redirect_to @deals, notice: 'Deal was successfully updated.'}
      format.json { head :no_content }
    else
      format.html { render action: 'edit' }
      format.json { render json: @deals.errors, status: :unprocessable_entity }
    end
  end
end

有趣。我用带有分隔符的数字\u助手尝试了相同的场景,这次它将“279900.00”保存为“279.00”,经过进一步测试,似乎self.PRICE的值在到达before\u save钩子之前已经是0.0了。什么在改变它?