Rename vars

This commit is contained in:
Alex Kotov 2020-10-17 05:14:03 +05:00
parent 184cf43241
commit 55b9883a54
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
3 changed files with 13 additions and 13 deletions

View File

@ -53,12 +53,12 @@ use rocket_contrib::templates::Template;
use rocket_csrf::CsrfToken;
#[get("/comments/new")]
fn new(csrf: CsrfToken) -> Template {
fn new(csrf_token: CsrfToken) -> Template {
// your code
}
#[post("/comments", data = "<form>")]
fn create(csrf: CsrfToken, form: Form<Comment>) -> Redirect {
fn create(csrf_token: CsrfToken, form: Form<Comment>) -> Redirect {
// your code
}
```
@ -69,8 +69,8 @@ to use it in [templates](https://rocket.rs/v0.4/guide/responses/#templates):
```rust
#[get("/comments/new")]
fn new(csrf: CsrfToken) -> Template {
let csrf_token: String = csrf.0;
fn new(csrf_token: CsrfToken) -> Template {
let authenticity_token: String = csrf_token.0;
// your code
}
@ -81,7 +81,7 @@ Add CSRF token to your HTML forms in
```html
<form method="post" action="/comments">
<input type="hidden" name="authenticity_token" value="{{ csrf_token }}"/>
<input type="hidden" name="authenticity_token" value="{{ authenticity_token }}"/>
<!-- your fields -->
</form>
```
@ -102,8 +102,8 @@ authenticity token:
```rust
#[post("/comments", data = "<form>")]
fn create(csrf: CsrfToken, form: Form<Comment>) -> Redirect {
if let Err(_) = csrf.verify(&form.authenticity_token) {
fn create(csrf_token: CsrfToken, form: Form<Comment>) -> Redirect {
if let Err(_) = csrf_token.verify(&form.authenticity_token) {
return Redirect::to(uri!(new));
}

View File

@ -10,7 +10,7 @@ use rocket_csrf::CsrfToken;
#[derive(Serialize)]
struct TemplateContext {
csrf_token: String,
authenticity_token: String,
flash: Option<String>,
}
@ -34,9 +34,9 @@ fn index() -> Redirect {
}
#[get("/comments/new")]
fn new(csrf: CsrfToken, flash: Option<FlashMessage>) -> Template {
fn new(csrf_token: CsrfToken, flash: Option<FlashMessage>) -> Template {
let template_context = TemplateContext {
csrf_token: csrf.0,
authenticity_token: csrf_token.0,
flash: flash.map(|msg| format!("{}! {}", msg.name(), msg.msg())),
};
@ -44,8 +44,8 @@ fn new(csrf: CsrfToken, flash: Option<FlashMessage>) -> Template {
}
#[post("/comments", data = "<form>")]
fn create(csrf: CsrfToken, form: Form<Comment>) -> Flash<Redirect> {
if let Err(_) = csrf.verify(&form.authenticity_token) {
fn create(csrf_token: CsrfToken, form: Form<Comment>) -> Flash<Redirect> {
if let Err(_) = csrf_token.verify(&form.authenticity_token) {
return Flash::error(
Redirect::to(uri!(new)),
"invalid authenticity token",

View File

@ -1,7 +1,7 @@
{{ flash }}
<form method="post" action="/comments">
<input type="hidden" name="authenticity_token" value="{{ csrf_token }}"/>
<input type="hidden" name="authenticity_token" value="{{ authenticity_token }}"/>
<input type="text" name="text"/>
<button type="submit">Submit</button>
</form>