首页 > 解决方案 > rails电子邮件激活页面添加电子邮件地址

问题描述

我用的是rails4.2.8和ruby2.5.0;

我有 email_confirm.html.erb ,当用户注册完成后,跳转到这个 html 页面,html 代码如下:

<% provide(:title, 'email activation') %>
<div class="container">
  <div class="col-md-8 col-md-offset-2 col-xs-12 col-sm-12 bgCo pdlr">
   <h3 class="pageItemTitle"><i class="fa fa-envelope text-yellow mr5"></i>email activation</h3>
   <h4 class="pd10 bgCo">Please Activation account:</h4>
   <div class="text-center">
   <span class="text-gray pbt20 ">The email had send to : <span class="text-yellow fs18"><%= current_user.email %></span></span>
 </div>
  <a class="btn btn-default pull-right" href="###">Go to the mailbox to check</a>
 </div>
</div>

关于注册的 user_controller.rb 如下:

def create
@user = User.new(user_params) 
if @user.save
  log_in @user
  flash[:success] = "还差一步就注册成功了!"
  redirect_to :email_confirm
else
  flash.now[:danger] = '注册失败,请输入正确内容!'
  render :new
end

结尾

我想得到邮箱公司对应的网址<%= current_user.email %>,这样as xxx@gmail.com对应mail.google.com,把邮箱公司的url添加到<a class="btn btn-default pull-right" href="###">Go to the mailbox to check</a>

我已经完成了逻辑内容,例如:

s = "dafaaf@gmail.com"

tumple = ["mail.qq.com","mail.126.com","mail.163.com","www.yeah.net","mail.sina.com.cn","mail.yahoo.com","mail.sohu.com","mail.aliyun.com","mail.google.com"]
y = s[/([A-Za-z0-9]+)(@)(.*+)/,3]
tumple.each_index do |i|
  if tumple[i].include?y
    print(tumple[i])
  else if y==="gmail.com"
     print("mail.google.com")
       end
   break
  end
end

但是现在,我怎样才能将这个逻辑内容添加到 rails(或控制器)?
请帮助我,非常感谢!

标签: ruby-on-rails

解决方案


假设您在页面中有current_user对象。email_confirm.html.erb在该对象中,您收到了用户的电子邮件。在这种情况下,您可以将邮箱代码复制到application_helper

application_helper.rb文件中:

def get_mailbox(email)
  email = email.split('@').last
  tumple = ["mail.qq.com","mail.126.com","mail.163.com","www.yeah.net","mail.sina.com.cn","mail.yahoo.com","mail.sohu.com","mail.aliyun.com","mail.google.com"]

  if email == "gmail.com"
    return "mail.google.com"
  else
    tumple.find { |t| return t if t.include? email }
  end
end

现在,在视图文件email_confirm.html.erb

<a class="btn btn-default pull-right" href="<%= get_mailbox(current_user.email) %>">Go to the mailbox to check</a>

希望能帮助到你!

注意我不检查你的逻辑,我也假设你有一个 current_user 对象。


推荐阅读