Oliver
Oliver

Reputation: 1181

Rails: trigger event in controller

In my app's controller once a record is created I want to automatically open the client's whatsapp, sms or email app (depending of the type of the new record).

My create action looks like this:

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

How can I achieve that?

Upvotes: 0

Views: 284

Answers (1)

murb
murb

Reputation: 1858

I gues you should be able to redirect to an app-specific url scheme. As an example of the idea:

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

You could make a redirect to an email and whatsapp with a predrafted message:

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

Sadly, the sms url scheme only supports a telephone number. Note that the mailto:-url scheme works almost everywhere an e-mail client is available, support for the whatsapp:-url scheme depends on the OS and whether the person has WhatsApp installed. Hence, know what you will be redirecting to!

Upvotes: 1

Related Questions