devise.en.yml文件中的变量

时间:2013-10-18 14:23:12

标签: ruby-on-rails ruby-on-rails-3 email devise

我想在设计确认邮件中添加用户的电子邮件地址,现在发送确认邮件后,设计显示“带有确认链接的邮件已发送到您的电子邮件地址。请打开链接到激活您的帐户。“但我想要的是插入已注册用户的电子邮件,以便它应该是”带有确认链接的消息已发送到您的#{params [:user] [:电子邮件]}。请打开链接以激活您的帐户。“

但不是显示电子邮件,而是显示文字。有什么建议吗?

2 个答案:

答案 0 :(得分:3)

今天解决了这个问题,所以想也应该为别人发一个答案。必须覆盖设计注册控制器使用此代码创建操作:

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."

P.S检查发生了什么的评论

答案 1 :(得分:0)

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}"