Tag: 插值

I18narrays中的插值

我在区域设置文件中使用数组,以便能够以区域设置特定的各种输出方法(ActionMailer模板,Prawn文档,HAML模板)生成文本块。 它工作得很好,但有时我想将变量传递给这些I18n调用。 但是,这不起作用。 假设我的语言环境文件如下所示: en: my_array: – “Line 1” – “Line 2” – “Line 3 has a %{variable}” – “Line 4” 我希望I18n.t(‘my_array’, :variable => ‘variable named variable’)如下: [“Line 1”, “Line 2”, “Line 3 has a variable named variable”, “Line 4”] 但是,输出是: [“Line 1”, “Line 2”, “Line 3 has a %{variable}”, “Line 4”] 检索数组后,我是否必须自己进行插值? 或者有更好的方法吗?

如何使validates_inclusion_of的错误消息显示允许的选项列表?

我有一个validates inclusion:在我的模型中,但我认为“未包含在列表中”的默认错误消息完全没有用。 如何让它在错误消息本身中显示允许的选项列表? (例如, “is not one of the allowed options (option 1, option 2, or option 3)” ? 更具体地说,获得以下测试的最优雅方法是: describe Person do describe ‘validation’ do describe ‘highest_degree’ do # Note: Uses matchers from shoulda gem it { should allow_value(‘High School’). for(:highest_degree) } it { should allow_value(‘Associates’). for(:highest_degree) } it { should allow_value(‘Bachelors’). for(:highest_degree) } […]

你能在HAML css中插入ruby变量吗?

我需要一系列类,.module1,.module2,… module(n)。 我还想使用css,ruby和HAML定义这些类: :css .mod1 { background-image: url(“#{extra.image}”); } 是否可以插入ruby变量以节省工作量? .module.mod”#{extra.id}” %h3 #{extra.title} %p #{extra.description} %a.btn-default{:href => “#”, :target => “_top”} enter now :css .mod#{extra.id} { background-image: url(“#{extra.image}”); }

如何从字符串插值中转义#{

我有一个heredoc,我使用#{}来插入一些其他字符串,但是有一个实例,我也想在我的heredoc中编写实际的文本#{some_ruby_stuff} ,而不进行插值。 有没有办法逃脱#{。 我试过“\”,但没有运气。 虽然它逃脱#{} ,但它也包含“\”: >> < ” development \#{RAILS_ENV}\n”