0

Hello I have a variable with a string here:

#[post("/select_date", data = "<time>")]
fn select_date(time: Form<SelectDate>) -> Result<Redirect, Error>{
    println!("{:#?}", time.date);
    let f_time = &time.date;
    Ok(Redirect::to("/"))
}

and I need to have the variable readable from here in the same file

#[get("/")]
async fn index(user: Option<User>) -> Template {
    let f_time = "";//here i would like to have that string
    Template::render("index", json!({ "user": user, "f_time ": f_time }))
}

I can't return the string in the select_date function because it needs to return the redirect to the index page otherwise it would be a black page.

Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
GranBoh
  • 67
  • 8
  • Have you tried passing the info on as `GET` parameter? – cafce25 Nov 22 '22 at 15:57
  • It's not just "another function". It's a handler for a completely separate request. If it were possible, ask yourself this: if two users call `select_data` and produce those time strings, when a request arrives to index, which of the two strings it should use? – Sergio Tulentsev Nov 22 '22 at 15:58
  • 1
    GET parameter would be the easiest to implement, but you can also set a cookie, for example. In any case, it will need to be a serialized form of the object, not a live reference to the object. – Sergio Tulentsev Nov 22 '22 at 16:00
  • 1
    That's impossible because when you return `Redirect::to("/")` the server code closes the first request. Then your client sends an _other_ request to `/`, but the server has no way to link the two requests. → You will need to return the value to the client (e.g. in a cookie) so that the client can send it back with the second request. – Jmb Nov 22 '22 at 16:01

0 Answers0