在 Spree (RoR) 中保存产品时,价格乘以 100 [英] Price gets multiplied by 100 when saving product in Spree (RoR)

查看:73
本文介绍了在 Spree (RoR) 中保存产品时,价格乘以 100的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Rails 3.1.3 和 Ruby 1.9.3 之上安装了在线购物框架 Spree.我还使用 Spree_i18n gem 来本地化商店.现在,每当我保存产品时,价格都会乘以 100.

例如,在管理区域,我输入价格 3.20.结果是值 320.如果我再次保存,它会更改为 32000,依此类推.

这是我本地化的 de_numbers.yml 以供参考:

---德:数字:货币:格式:格式:%u%n"单位:€"精度:2分隔器: '.'分隔符:','

我想不出我的设置有什么不寻常的地方,所以我想知道为什么这不是一个常见问题.任何帮助将不胜感激.

解决方案

spree-core: 产品表单不处理显示 product.price 和 product.cost_price 与 I18n/本地化相关的内容.为了解决这个问题,你需要修改核心.我将向 Spree Core 团队发布有关此事的消息,但在此期间,我已经测试了此修复程序,它应该可以工作.

/gems/spree_core-1.0.0/app/views/spree/admin/products/_form.html.erb中,您需要修改以下几行:

<%= f.text_field :price, :value =>number_with_precision(@product.price, :precision => 2) %>

变成这样:

<%= f.text_field :price, :value =>number_with_precision(@product.price, :precision => I18n.t('number.currency.format.precision'), :separator => I18n.t('number.currency.format.separator'), :delimiter => I18n.t('number.currency.format.delimiter')) %>

还有这个:

<%= f.text_field :cost_price, :value =>number_with_precision(@product.cost_price, :precision => 2) %>

变成这样:

<%= f.text_field :cost_price, :value =>number_with_precision(@product.cost_price, :precision => I18n.t('number.currency.format.precision'), :separator => I18n.t('number.currency.format.separator'), :delimiter => I18n.t('number.currency.format.delimiter')) %>

本质上,我们让它处理 I18n 潜在值.

原文:

我已经完全复制了您的文件,并尝试了一些测试来重新创建此文件(创建新产品、新产品变体、更改产品价格、成本价格等).要重新创建它,您需要创建一个 de_numbers.yml,并使用config.default_locale = 'de'"在 Spree 初始值设定项中将您的本地化翻转为 'de'

以下是一些建议的修复:

  1. 确保您运行 bundle install
  2. 在您的 Gemfile 中,确保您使用的是最新版本的 i18n (

<块引用>

gem 'spree_i18n', :git => 'git://github.com/spree/spree_i18n.git')

  1. 将您的空格修正为 2 个空格,而不是制表符(这可能是一个无法读取您的 i18n 值的空格问题)
  2. 进入 rails 控制台,并注销值(即

<块引用>

I18n.t('number.currency.format.unit')

  1. 先尝试使其在en"语言环境中工作,然后在de"语言环境中工作.
  2. 首先将您的值放入de.yml"或en.yml"中,然后再放入de_currency.yml"文件中,看看它们是否有效.

I installed the online shopping framework Spree on top of Rails 3.1.3 and Ruby 1.9.3. I also use the Spree_i18n gem to localize the shop. Now, whenever I save a product, the price is multiplied by 100.

For example, in the admin area, I type the price 3.20. That results in the value 320. If I save again, it changes to 32000 and so on.

Here is my localized de_numbers.yml for reference:

---
de:
  number:
    currency:
      format:
        format: "%u%n"
        unit: "€"
        precision: 2
        separator: '.'
        delimiter: ','

I can not think of anything unusual in my setup so I wonder why this is not a common problem. Any help will be appreciated.

解决方案

EDIT:

spree-core: the Product form is not handling displaying product.price and product.cost_price with regards to I18n / localization. To fix this to work for you, you'll need to modify the core. I'm going to post to the Spree Core team about this, but in the interim, I've tested this fix and it should work.

In /gems/spree_core-1.0.0/app/views/spree/admin/products/_form.html.erb, you will need to modify these lines:

<%= f.text_field :price, :value => number_with_precision(@product.price, :precision => 2) %>

to be this:

<%= f.text_field :price, :value => number_with_precision(@product.price, :precision => I18n.t('number.currency.format.precision'), :separator => I18n.t('number.currency.format.separator'), :delimiter => I18n.t('number.currency.format.delimiter')) %>

and this:

<%= f.text_field :cost_price, :value => number_with_precision(@product.cost_price, :precision => 2) %>

to be this:

<%= f.text_field :cost_price, :value => number_with_precision(@product.cost_price, :precision => I18n.t('number.currency.format.precision'), :separator => I18n.t('number.currency.format.separator'), :delimiter => I18n.t('number.currency.format.delimiter')) %>

Essentially, we're making it handle I18n potential values.

ORIGINAL:

I've duplicated your file exactly, and tried a few tests to recreate this (create a new product, new product variant, change the product price, cost price, etc.). To re-create this, you need to create a de_numbers.yml, and flip your localization to be 'de' in the Spree initializer with "config.default_locale = 'de'"

Here are some suggested fixes:

  1. make sure you run bundle install
  2. in your Gemfile, make sure you're using the latest version of i18n (

gem 'spree_i18n', :git => 'git://github.com/spree/spree_i18n.git')

  1. fix your whitespace to be 2 spaces, instead of tabs (this is potentially a white-space issue where it can't read your i18n values)
  2. Go into the rails console, and log out the values (i.e.

I18n.t('number.currency.format.unit')

  1. Try getting this to work in "en" locale first and then "de".
  2. Put your values into the "de.yml" or "en.yml" first and see if they work before putting into a "de_currency.yml" file.

这篇关于在 Spree (RoR) 中保存产品时,价格乘以 100的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆