2017-06-14 57 views
0

在我的应用程序的控制器中创建一条记录后,我想自动打开客户端的whatsapp,sms或电子邮件应用程序(取决于新记录的类型)。Rails:控制器中的触发事件

我创建操作是这样的:

def create 
    session[:return_to] ||= request.referer 

    @token = SecureRandom.uuid 
    @new_token = Token.new(:token_value => @token, :sender_id => current_user.id, :card_id => params[:card_id], :type => params[:type])  

    respond_to do |format| 
     format.html { redirect_to session.delete(:return_to), notice: "Token " + @new_token.token_value + " was successfully created." } 
    end 

    if @new_token.type == "sms" 
     #open client's sms app and autofill sms body with @new_token.token_value 

    elsif @new_token.type == "email" 
     #open client's email app and autofill subject + body with @new_token.token_value 

    elsif @new_token.type == "whatsapp" 
     #open client's whatsapp and autofill body with @new_token.token_value 

    end 

    end 

我怎样才能做到这一点?

回答

1

我觉得你应该能够重定向到一个特定于应用程序的url方案。为理念的一个例子:

<html> 
 
<meta http-equiv="refresh" content="0; url=mailto:[email protected]?body=token&subject=Your new token" /> 
 
</html>

你可以做一个重定向到电子邮件和WhatsApp的一个predrafted消息:

redirect_to 'mailto:[email protected]?body=token&subject=Your new token' 
redirect_to 'whatsapp://send?text=token' 

可悲的是,sms url scheme only supports a telephone number。请注意,mailto: -url方案几乎可在电子邮件客户端可用的任何地方运行,支持whatsapp: -url方案取决于操作系统以及是否安装了WhatsApp。因此,知道你将重定向到什么!

+0

它的工作原理!但是,只有当我指定一个电子邮件地址时才会这样做。如果我将它留空 - 比如'mailto:?body ...“,它不起作用。是否有解决方案(因为在我的情况下,地址应该是空的) – Oliver

+0

您应该可以这样做:https: //stackoverflow.com/questions/3540664/how-do-you-create-a-mailto-link-without-the-to-part ...但也许你可以解决它与%20(一个空格?) – murb

+0

我尝试了两种方法,但是只有在mailtto之后有一个“@”时,redirect_to才有效: – Oliver

相关问题