载波0.9.0的自定义错误消息不适用于Rails 4 [英] Custom error message for carrierwave 0.9.0 doesn't work for Rails 4

查看:51
本文介绍了载波0.9.0的自定义错误消息不适用于Rails 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Rails 4上使用了 carrierwave 0.9.0 ,并且尝试制作自定义错误消息。经过一番搜索之后,我发现了这个 answer

I use carrierwave 0.9.0 with Rails 4 and I'm trying to make a custom error message. After doing some search, I found this answer:

en:
  errors:
    messages:
      extension_white_list_error: 'My Custom Message'

我通过转到 config / locales / en.yml 进行了尝试上面的代码,但没有任何变化。错误消息仍然相同。

I tried this by going to config/locales/en.yml then adding the code above, but nothing changed. The error message is still the same.

在github 此处,但没有答案,我认为此问题仅与Rails 4有关,但不确定,使用Rails 4时是否会遇到相同的问题?

There is also the same issue on github here, but no answers, I think this problem is specific just with Rails 4 but not sure, have you the same problem when you use Rails 4?

推荐答案

关于i18n文件,Rails 4.0似乎存在一些加载问题

There seem be some loading issue with Rails 4.0 with regards to i18n files

/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327@rails3_2/gems/activesupport-4.0.0/lib/active_support/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327@rails3_2/gems/activemodel-4.0.0/lib/active_model/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327@rails3_2/gems/activerecord-4.0.0/lib/active_record/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327@rails3_2/gems/actionpack-4.0.0/lib/action_view/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327@rails3_2/gems/carrierwave-0.9.0/lib/carrierwave/locale/en.yml
/Users/joshianiket22/carrierwave_tester/config/locales/en.yml


在Rails 4.0中看到的内容


/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327@rails3_2/gems/activesupport-3.2.11/lib/active_support/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327@rails3_2/gems/activemodel-3.2.11/lib/active_model/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327@rails3_2/gems/activerecord-3.2.11/lib/active_record/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327@rails3_2/gems/actionpack-3.2.11/lib/action_view/locale/en.yml
/Users/joshianiket22/workspace/zenjavi/carrierwave_tester/config/locales/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327@rails3_2/gems/carrierwave-0.9.0/lib/carrierwave/validations/../locale/en.yml

一个人可以清楚地看到两者之间的区别在特定于应用程序的 en.yml 之后加载了两个载波 en.yml ,这是您的问题

One can clear see the difference between the two the carrierwave en.yml is loaded after a the application specific en.yml and there is your issue

我建议在那里除非您更改rails应用程序中的load_paths以及如何设法按预期方式更改load_paths的顺序,否则绝非易事。

I suggest there is no easy way unless you the change the load_paths in rails application and some how manage to change the order of load_paths of as expected

我已经通过此处。完全由Carrierwave员工敬畏地做出决定

I have given a pull request over here. Completely at awe of Carrierwave guys to decide on it

我一直在尽早为您提供此解决方案,但仍然愿意糟糕的是这里您可以做什么

I was refraining in giving you this solution earlier but still if you want it that bad here what you can do

在lib目录中定义一个文件(假设为auto_load_i18n.rb)并为自动加载分配lib路径(在 application.rb 中) )

define a file in lib directory(let say auto_load_i18n.rb) and assign the lib path to autoload (in application.rb)

config.autoload_paths += %w(#{config.root}/lib)

内部 auto_load_i18n.rb 写下

I18n.load_path.delete(Rails.root.join("config/locales/en.yml").to_s)
I18n.load_path << Rails.root.join("config/locales/en.yml").to_s

并需要lib文件位于 application_controller.rb

And require the lib file at the top of your application_controller.rb

require 'auto_load_i18n'
class ApplicationController < ActionController::Base

然后我猜一切都会正常工作

and I guess everything would work then

您现在可以了解为什么我不提供这种可能的解决方案:)

You can now understand as to why I was refraining in giving this as a possible solution :)

希望获得帮助

这篇关于载波0.9.0的自定义错误消息不适用于Rails 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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