Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用'接受形式上的小数;步骤';:十进制未正确保存在数据库中_Ruby On Rails_Ruby On Rails 4_Decimal - Fatal编程技术网

Ruby on rails 使用'接受形式上的小数;步骤';:十进制未正确保存在数据库中

Ruby on rails 使用'接受形式上的小数;步骤';:十进制未正确保存在数据库中,ruby-on-rails,ruby-on-rails-4,decimal,Ruby On Rails,Ruby On Rails 4,Decimal,我试图在我的表格中接受小数: <%= f.label :price %><br /> <%= f.number_field :price, :step => 0.01 %> 0.01 %> 但只要我在客户端的表单中输入13.75这样的数字,它就会将数字四舍五入到13.00美元。这是显示价格的代码: <%= number_to_currency(@product.price) %> 我不认为数字对货币的方法与此有关,但我想这

我试图在我的表格中接受小数:

  <%= f.label :price %><br />
  <%= f.number_field :price, :step => 0.01 %>

0.01 %>
但只要我在客户端的表单中输入13.75这样的数字,它就会将数字四舍五入到13.00美元。这是显示价格的代码:

<%= number_to_currency(@product.price) %>

我不认为数字对货币的方法与此有关,但我想这是值得注意的


任何帮助都将不胜感激,谢谢

在schema.rb文件中,您应该

t.float  "price"
而不是

t.integer  "price"

为此,运行
rails g migration change\u data\u type\u for\u fieldname

现在,一个新的迁移文件应该出现在db/migrate中:

class ChangeDataTypeForFieldName < ActiveRecord::Migration
  def up
  end

  def down
  end
end
class ChangeDataTypeForFieldName
用适当的名称填写:

class ChangeDataTypeForFieldName < ActiveRecord::Migration
def self.up
    change_table :table_name do |t|
      t.change :field , :new_datatype
    end
  end
  def self.down
    change_table :table_name do |t|
      t.change :field, :old_data_type
    end
  end
end
class ChangeDataTypeForFieldName

运行
rake db:migrate

您的price属性是否可能是数据库/模型中的整数?如果这不起作用,您能否编辑您的问题以包含提交给服务器的参数?另外,当您删除数字\u to \u currency时会发生什么情况?能否包含schema.rb文件的相关部分?是的,我将price属性设置为整数,这导致了问题。将其更改为浮动。谢谢大家的回复!
class ChangeDataTypeForFieldName < ActiveRecord::Migration
def self.up
    change_table :table_name do |t|
      t.change :field , :new_datatype
    end
  end
  def self.down
    change_table :table_name do |t|
      t.change :field, :old_data_type
    end
  end
end