简体   繁体   English

设计自定义Flash消息

[英]devise custom flash message

I would like to access current_user.username in my flash message after registering. 注册后,我想在我的Flash消息中访问current_user.username。 How do I go about doing this? 我该怎么做呢? Can this be done in the devise.en.yml file? 可以在devise.en.yml文件中完成吗?

I think you have to overwrite registration controller in order to customize your flash message. 我认为您必须覆盖注册控制器才能自定义Flash消息。

If you take a look at devise.en.yml file, you can see that some variables like %{resource} or %{count} are used. 如果查看devise.en.yml文件,可以看到使用了一些变量,例如%{resource}%{count} By taking a look at original registration controller, you can see this code ( check here ) 通过查看原始注册控制器,您可以看到此代码( 在此处检查

# POST /resource
def create
build_resource(sign_up_params)

if resource.save
  yield resource if block_given?
  if resource.active_for_authentication?
    set_flash_message :notice, :signed_up if is_flashing_format?
    sign_up(resource_name, resource)
    respond_with resource, location: after_sign_up_path_for(resource)
  else
    set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
    expire_data_after_sign_in!
    respond_with resource, location: after_inactive_sign_up_path_for(resource)
  end
else
  clean_up_passwords resource
  respond_with resource
end

end 结束

I would rewrite this controller and add this line 我将重写此控制器并添加此行

set_flash_message :notice, :signed_up, :username => resource.username if is_flashing_format?

Then in your devise.en.yml file, you should be able to use something like that 然后,在您的devise.en.yml文件中,您应该可以使用类似的内容

devise:
  registrations:
    signed_up: 'oh hello %{username}'

Tell me if that worked. 告诉我是否可行。

If you need hint on how to rewrite Devise controller, take a look at this 如果您需要提示如何改写设计控制器,看看这个

Hope it helped. 希望能有所帮助。

===== UPDATE ===== =====更新=====

I tested it and it worked. 我对其进行了测试,并且效果良好。

Okay so if we want to go deeper, we can check lib/devise/controllers/internal_helpers.rb : 好的,如果我们想更深入一点,可以检查lib/devise/controllers/internal_helpers.rb

# Sets the flash message with :key, using I18n. By default you are able
# to setup your messages using specific resource scope, and if no one is
# found we look to default scope.
# Example (i18n locale file):
#
# en:
# devise:
# passwords:
# #default_scope_messages - only if resource_scope is not found
# user:
# #resource_scope_messages
#
# Please refer to README or en.yml locale file to check what messages are
# available.
def set_flash_message(key, kind, options={}) #:nodoc:
  options[:scope] = "devise.#{controller_name}"
  options[:default] = Array(options[:default]).unshift(kind.to_sym)
  options[:resource_name] = resource_name
  message = I18n.t("#{resource_name}.#{kind}", options)
  flash[key] = message if message.present?
end

But please update your code so we can see what's wrong. 但是,请更新您的代码,以便我们找出问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM