首页 > 解决方案 > 将 url 添加到字符串

问题描述

在我的应用程序中,我希望能够根据用户偏好通过电子邮件或短信发送确认消息。在这些消息中,有一个链接供他们单击以确认。

对于电子邮件,我在视图中添加了这样的内容:

<%= link_to "Click here to confirm", confirmation_url(param stuff) %>

但是,我不确定如何对短信做同样的事情。我正在使用 twilio-ruby gem,我只有一个可以发送消息的字符串。像这样的东西不起作用:

client = Twilio::REST::Client.new
client.messages.create({
  from: ENV['TWILIO_PHONE_NUMBER'],
  to: phone_number,
  body: "Welcome to MySite.  Please click this link to confirm: #{ confirmation_url(param stuff) }
})

基本上我了解 link_to 如何获取 URL,但如果我不在 html 视图上下文中,我不知道该怎么做。

感谢任何帮助!

标签: ruby-on-rails

解决方案


您可以使用

Rails.application.routes.url_helpers.confirmation_url(param stuff)

推荐阅读