Rails I18n in verification.rb verify method does not work?

雨燕双飞 提交于 2019-12-06 06:00:54

Since verify is a class method called at the class definition level, its arguments, including the I18n.t call, are evaluated as the controller is loaded. In order for I18n.t to work properly, it needs to be evaluated each time the relevant controller actions are executed. verify does not have the ability to do this.

Instead, I suggest you use a before filter:

before_filter :verify_session, :only => [:destroy, :create, :update, :new, :comment]

def verify_session
  unless session[:user_id]
    flash[:error] = I18n.t('Exceptions.not_logged_in')
    redirect_to :controller => 'main', :action => 'index'
  end
end

Also, note that verify has been deprecated and moved to a plugin in Rails 3, while before filters continue to work.

The translation is in the wrong place; the translated version of the message is strictly a user interface issue so the translation should occur at the user interface level. The FlashHash should only contain error condition identifiers of some sort, the user interface code should deal with converting that to something a human can understand:

verify :only => [ :destroy, :create, :update, :new, :comment ],
     :session => :user_id,
     :add_flash => { :error => 'Exceptions.not_logged_in' },
     :redirect_to => { :controller => 'main' , :action => 'index' }

And then, later in some ERB or whatever:

<% if flash[:error] %>
    <p class="error"><%= I18n.t(flash[:error]) %></p>
<% end %>

This approach also makes it easy to push error IDs all the way to a REST client or JavaScript front end without forcing them to deal with parsing ever-changing error messages:

if((flash.error || '') == 'Exceptions.not_logged_in')
    sammy.setLocation('#/sign_in');

I can't be the only person whose error handling code broke after an upgrade because someone changed an error message and didn't offer any error mechanism that was meant to be consumed by software rather than humans.

Put the call to I18n.t in parens

add_flash => { :error => (I18n.t(:'Exceptions.not_logged_in')) },

I think it might have something to do with that I18n.t call being run only once when the file is initially loaded on app startup. You could try wrapping it in a Proc or lambda or maybe in a separate method. e.g. something like:

:add_flash => lambda { { :error => I18n.t(:'Exceptions.not_logged_in') } }

No idea if that'll work, but it might get you closer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!