Skip to content

Commit

Permalink
feat(README.md): add support for retrieving and updating message titl…
Browse files Browse the repository at this point in the history
…e in edit route of MessagesController
  • Loading branch information
marcuxyz committed Nov 9, 2023
1 parent 8b24259 commit ad90341
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,25 @@ The previous example describes the `hi(self)` will be called every times that th
we know that the HTML form doesn't send payload to `action` with `put` or `delete` method as attribute of `form tag`. But,
the `FLASK MVC` does the work for you, everything you need is add the tag in HTML template. Look:


```python
# app/controllers/messages_controller.py

from flask import render_template, redirect, url_for, flash
from flask import render_template, redirect, url_for, flash, request

class MessagesController:
def edit(self, id):
return render_template("messages/edit.html")
message = Message.query.get(id)

return render_template("messages/edit.html", message=message)

def update(self, id):
message = Message.query.get(id)
message.title = request.form.get('title')

db.session.add(message)
db.session.commit()
flash('Message sent successfully!')

return redirect(url_for(".edit"))
```

Expand All @@ -200,16 +207,16 @@ class MessagesController:
<!-- app/views/messages/edit.html -->
{% block content %}
<form action="{{ url_for('messages.update', id=1) }}" method="POST">
<input type="hidden" name="_method" value="PUT">
<form action="{{ url_for('messages.update', id=message.id) }}" method="POST">
<input type="hidden" name="_method" value="put">
<input type="text" name="title" id="title" value="Yeahh!">
<input type="submit" value="send">
</form>
{% endblock %}
```

The `<input type="hidden" name="_method" value="PUT">` is necessary to work sucessfully!
The `<input type="hidden" name="_method" value="put">` is necessary to work sucessfully!

## Views

Expand Down

0 comments on commit ad90341

Please sign in to comment.