Reputation: 11
I'm trying to recreate a simple note app I built in JavaScript in Rust but I've ran into an issue with trying to delete an item from my database using form data. I've followed the docs for help but I have no idea how to implement it, I'm pretty new to Rust and Rocket.rs so im quite lost.
So far Im able to add an item item to the database and this works fine
#[post("/", data = "<note>")]
fn add(note: Form<NoteForm>) -> Redirect {
println!("Note: {}", note.note);
let conn = Connection::open("notes.db").unwrap();
conn.execute("INSERT INTO notes (note) VALUES (?)", &[note.note.as_str()]).unwrap();
Redirect::to("/")
}
But when i try to implement something similar for my delete button
#[post("/delete", data = "<noteid>")]
fn delete(noteid: Form<NoteID>) -> Redirect {
println!("Note: {}", noteid.id);
let conn = Connection::open("notes.db").unwrap();
conn.execute("DELETE FROM notes WHERE rowid = ?", &[noteid.id.to_string().as_str()]).unwrap();
Redirect::to("/")
}
I get this error when i try to press the button
POST /delete application/x-www-form-urlencoded:
>> Matched: (delete) POST /delete
>> Data guard `Form < NoteID >` failed: Errors([Error { name: Some("id"), value: None, kind: Missing, entity: Field }]).
>> Outcome: Failure
>> No 422 catcher registered. Using Rocket default.
For context this is what the html looks like
<h1>AAF Notebook</h1>
<form method='POST'>
<label>Note: <input name='note' value=''></label>
<button>Add</button>
</form>
<ul class='notes'>
<li class='notes'>note 1 <form method='POST' action='/delete'>
<button name='noteid' value='1'>Delete</button>
</li>
</form>
I would post an image but i dont have enough reputation
EDIT: I changed the button name from 'Delete' to 'noteid' to refect the rust code but the same error still persists
Upvotes: 0
Views: 121
Reputation: 11
I seemed to have had my variable names all out of wack changing all the names to noteid
instead of id
fixed the issue
#[derive(FromForm)]
struct NoteID {
noteid: i32,
}
#[post("/delete", data = "<noteid>")]
fn delete(noteid: Form<NoteID>) -> Redirect {
println!("Note: {}", noteid.noteid);
let conn = Connection::open("notes.db").unwrap();
conn.execute("DELETE FROM notes WHERE rowid = ?", &[noteid.noteid.to_string().as_str()]).unwrap();
Redirect::to("/")
}
Upvotes: 0