Skip to main content

url

Function url 

Source
pub fn url(ctx: &Ctx, path: &str) -> Result<String>
Expand description

An absolute URL for path on the app’s public address, the APP_URL variable (Rails’ _url helpers in mailers).

For links and images in emails, which mail clients open outside the app: url(ctx, "/posts/1") is https://shop.example.com/posts/1. A path that is already absolute is returned as is. It reads a variable, no binding call, so mailers and jobs can use it outside any request.

§Errors

Error::Internal (500) when APP_URL is unset or not an http(s) address, naming the fix.

§Examples

use ocre::{Ctx, Result, mail::{self, Email}};

fn reset_email(ctx: &Ctx, to: &str, token: &str) -> Result<Email> {
    let link = mail::url(ctx, &format!("/password/reset/{token}"))?;
    let logo = mail::url(ctx, "/images/logo.png")?;
    Ok(Email::new(to, "Reset your password", format!("Open {link}"))
        .html(format!("<img src=\"{logo}\" alt=\"\"><p><a href=\"{link}\">Reset your password</a></p>")))
}