【问题标题】:Rails 3.1 invalid multibyte char (UTF-8)Rails 3.1 无效的多字节字符 (UTF-8)
【发布时间】:2011-07-31 21:20:27
【问题描述】:

我正在尝试传递这个简单的断言

assert_select '.price', /\€[,\d]+\.\d\d/

在视图中 .price 类的代码是

<span class="price">
 <%= number_to_currency(product.price, options = {:format => "%u%n", :unit => "&euro;"}) %> </span>

当我使用默认的&lt;%= number_to_currency(product.price) %&gt; 时,一切都很好。当我切换到欧元时,它就不起作用了。

这是错误信息:

/Users/noapologize/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0.rc4/lib/active_support/dependencies.rb:237:in `require': /Users/noapologize/rails_projects/depot/test/functional/store_controller_test.rb:11: invalid multibyte char (UTF-8) (SyntaxError)
/Users/noapologize/rails_projects/depot/test/functional/store_controller_test.rb:11: invalid multibyte char (UTF-8)
/Users/noapologize/rails_projects/depot/test/functional/store_controller_test.rb:11: syntax error, unexpected $end, expecting keyword_end
    assert_select '.price', /\€[,\d]+\.\d\d/

我想我写这个 assert_select 的方式是错误的。有人可以启发我吗?

感谢您的宝贵时间。

【问题讨论】:

    标签: utf-8 ruby-on-rails-3.1 ruby-1.9


    【解决方案1】:

    如果您从欧元符号中删除转义斜线,则此正则表达式将正确编译并工作。

    【讨论】:

    • 谢谢!现在可以了。这很奇怪,因为实用书就是这么写的。我不明白。为什么它适用于美元而不适用于欧元?是字符的字节长度还是什么?再次感谢您的回答和您的时间。
    • 这是因为美元在正则表达式中用作命令字符,所以你需要转义它。如果 Pragprog 的书说你需要逃避欧元,你可以针对他们的书提出错误 ;-)
    猜你喜欢
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多