Rails 金钱宝石和表单生成器

发布于 2024-10-14 22:44:57 字数 510 浏览 10 评论 0原文

我的表单和 money gem 存在问题。

这是我的问题:

  1. 我创建一个具有“金额”字段(映射到货币对象)的记录。假设我输入 10(美元)。
  2. 货币宝石将其转换为 1000(美分)
  3. 我编辑相同的记录,表单将金额字段预填充为 1000
  4. 如果我保存记录而不更改任何内容,它将把 1000(美元)转换为 100000(美分)

如何做我让它以美元而不是美分显示预先填充的金额?

编辑:

我尝试像这样编辑 _form.html:

= f.text_field(:amount, :to_money)

并且收到此错误:

undefined method `merge' for :to_money:Symbol

I'm having an issue with the forms and the money gem.

This is my problem:

  1. I create a record which has an "amount" field (mapped to money object). Let's say I enter 10 (dollars).
  2. The money gem converts it to 1000 (cents)
  3. I edit the same record and the form pre-populates the amount field as 1000
  4. If I save the record without changing anything, it will convert the 1000 (dollars) to 100000 (cents)

How do I make it display the pre-populated amount in dollars instead of cents?

Edit:

I tried editing the _form.html like this:

= f.text_field(:amount, :to_money)

and I get this error:

undefined method `merge' for :to_money:Symbol

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青柠芒果 2024-10-21 22:44:57

假设迁移如下:

class CreateItems < ActiveRecord::Migration
  def self.up
    create_table :items do |t|
      t.integer :cents
      t.string :currency
      t.timestamps
    end
  end

  def self.down
    drop_table :items
  end
end

模型如下:

class Item < ActiveRecord::Base
  composed_of :amount,
    :class_name  => "Money",
    :mapping     => [%w(cents cents), %w(currency currency_as_string)],
    :constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) },
    :converter   => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't conver #{value.class} to Money") }
end

那么这个表单代码应该可以完美工作(我刚刚在 Rails 3.0.3 下测试),每次保存/编辑时都能正确显示和保存美元金额。 (这是使用默认的脚手架更新/创建方法)。

<%= form_for(@item) do |f| %>
  <div class="field">
    <%= f.label :amount %><br />
    <%= f.text_field :amount %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Given a migration as follows:

class CreateItems < ActiveRecord::Migration
  def self.up
    create_table :items do |t|
      t.integer :cents
      t.string :currency
      t.timestamps
    end
  end

  def self.down
    drop_table :items
  end
end

And a model as follows:

class Item < ActiveRecord::Base
  composed_of :amount,
    :class_name  => "Money",
    :mapping     => [%w(cents cents), %w(currency currency_as_string)],
    :constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) },
    :converter   => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't conver #{value.class} to Money") }
end

Then this form code should work perfectly (I just tested under Rails 3.0.3), properly displaying and saving the dollar amount every time you save/edit. (This is using the default scaffold update/create methods).

<%= form_for(@item) do |f| %>
  <div class="field">
    <%= f.label :amount %><br />
    <%= f.text_field :amount %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
尸血腥色 2024-10-21 22:44:57

您现在可以直接编辑货币化字段 (money-rails 1.3.0):

# add migration
add_column :products, :price, :price_cents

# set monetize for this field inside the model
class Product
  monetize :price_cents
end

# inside form use .price instead of .price_cents method
f.text_field :price

请参阅 https://stackoverflow.com/a/30763084/46039

You can now edit monetized fields directly (money-rails 1.3.0):

# add migration
add_column :products, :price, :price_cents

# set monetize for this field inside the model
class Product
  monetize :price_cents
end

# inside form use .price instead of .price_cents method
f.text_field :price

See https://stackoverflow.com/a/30763084/46039

逆流 2024-10-21 22:44:57

如果表中有多个货币字段,并且不能将它们全部命名为“cents”。

class CreateItems < ActiveRecord::Migration
  def self.up
    create_table :items do |t|
      t.integer :purchase_price_cents
      t.string :currency
      t.timestamps
    end
  end

  def self.down
    drop_table :items
  end
end

这会将你的模型更改为

class Item < ActiveRecord::Base

  composed_of :purchase_price,
    :class_name  => "Money",
    :mapping     => [%w(purchase_price_cents cents), %w(currency currency_as_string)],
    :constructor => Proc.new { |purchase_price_cents, currency| Money.new(purchase_price_cents || 0, currency || Money.default_currency) },
    :converter   => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") }

end

If you have multiple money fields in your table and you can't name them all "cents".

class CreateItems < ActiveRecord::Migration
  def self.up
    create_table :items do |t|
      t.integer :purchase_price_cents
      t.string :currency
      t.timestamps
    end
  end

  def self.down
    drop_table :items
  end
end

which would change your model to

class Item < ActiveRecord::Base

  composed_of :purchase_price,
    :class_name  => "Money",
    :mapping     => [%w(purchase_price_cents cents), %w(currency currency_as_string)],
    :constructor => Proc.new { |purchase_price_cents, currency| Money.new(purchase_price_cents || 0, currency || Money.default_currency) },
    :converter   => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") }

end
捂风挽笑 2024-10-21 22:44:57

货币化和简单形式,步骤如下:

  1. 迁移

add_monetize :table, :amount

  1. 带验证的模型

monetize :amount_cents, allowed_nil: true, numericity: {greater_than: 0}

  1. 控制器允许参数(这里不要使用 amount_cents)

params.require(: model).permit(:amount)

  1. 简单的表单输入,

保存时将以分形式保存在数据库中的 amount_cents 列中

monetizing and the simple form, the steps as follows:

  1. Migration

add_monetize :table, :amount

  1. Model with validation

monetize :amount_cents, allow_nil: true, numericality: {greater_than: 0}

  1. Controller permit params (dont use amount_cents here)

params.require(:model).permit(:amount)

  1. simple form input

when it's saved it will be saved in cents in amount_cents column in db

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文