rails email激活页面添加电子邮件地址

时间:2018-04-30 15:29:56

标签: ruby-on-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,并将邮箱公司的网址添加到<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(或控制器)? 请帮助我,非常感谢!

2 个答案:

答案 0 :(得分:0)

假设您在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>

希望它有所帮助!

<强> N.B。我不会检查你的逻辑,我也假设你有一个current_user对象。

答案 1 :(得分:0)

你真的应该以更好的红宝石方式重构你的逻辑

def get_mailbox
  email = current_user.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"
    print "mail.google.com"
  else
    tumple.find { |t| print t if t.include? email }
  end
end