我想在设计确认消息中添加用户的电子邮件地址,现在在发送确认邮件后,设计显示“带有确认链接的消息已发送到您的电子邮件地址。请打开链接以激活您的帐户。” 但我想要的是插入已注册用户的电子邮件,因此它应该类似于“带有确认链接的消息已发送到您的 #{params[:user][:email]}。请打开链接以激活您的帐户。”
但不是显示电子邮件,而是显示文本。有什么建议怎么做吗?
我想在设计确认消息中添加用户的电子邮件地址,现在在发送确认邮件后,设计显示“带有确认链接的消息已发送到您的电子邮件地址。请打开链接以激活您的帐户。” 但我想要的是插入已注册用户的电子邮件,因此它应该类似于“带有确认链接的消息已发送到您的 #{params[:user][:email]}。请打开链接以激活您的帐户。”
但不是显示电子邮件,而是显示文本。有什么建议怎么做吗?
今天解决了这个问题,所以认为也应该为其他人发布答案。必须使用以下代码覆盖设计注册控制器创建操作:
class RegistrationsController < Devise::RegistrationsController
# POST /resource
def create
build_resource(sign_up_params)
if resource.save
# this block will be used when user is saved in database
if resource.active_for_authentication?
# this block will be used when user is active or not required to be confirmed
set_flash_message :notice, :signed_up if is_navigational_format?
sign_up(resource_name, resource)
respond_with resource, :location => after_sign_up_path_for(resource)
else
# this block will be used when user is required to be confirmed
user_flash_msg if is_navigational_format? #created a custom method to set flash message
expire_session_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
# this block is used when validation fails
clean_up_passwords resource
respond_with resource
end
end
private
# set custom flash message for unconfirmed user
def user_flash_msg
if resource.inactive_message == :unconfirmed
#check for inactive_message and pass email variable to devise locals message
set_flash_message :notice, :"signed_up_but_unconfirmed", email: resource.email
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}"
end
end
end
然后在 devise.en.yml 文件中进行必要的更改,我们都准备好了
en:
devise:
registrations:
signed_up_but_unconfirmed: "A confirmation link has been sent to %{email}. Click the link to activate your account."
PS 检查评论以了解正在发生的事情
i18n 的 Rails 指南涵盖了这种情况: http: //guides.rubyonrails.org/i18n.html#passing-variables-to-translations
在视图中:
# app/views/home/index.html.erb
<%=t 'greet_username', user: "Bill", message: "Goodbye" %>
在语言环境文件中:
# config/locales/en.yml
en:
greet_username: "%{message}, %{user}!"
更新 :
# app/views/home/index.html.erb
<%=t 'email_message', email: params[:user][:email] %>
# config/locales/en.yml
en:
email_message: "Your email address is : %{email}"