ใน Pylonsbook มีตัวอย่างเขียน code สำหรับ delete ไว้เรียบร้อยแล้ว http://pylonsbook.com/alpha1/simplesite_tutorial แต่ว่าผมก็อยากจะดัดแปลงนิดหน่อย เพราะใช้ Elixir แล้วก็อยากได้ dialog เพื่อยืนยันก่อนลบด้วย
จากตอนเดิมที่ใช้ Pagination list item ต่างๆ ออกมาได้แล้วก็แก้ code นิดหน่อยให้คล้ายกับ CakePHP เพื่อที่จะได้ลบ item ต่อไป
แก้ template ใน person/index.html
...
% for item in c.corpora_paginator:
<tr>
<td>${item.id}</td>
<td>${item.name}</td>
<td>
<!-- เพิ่มส่วน delete เข้ามา -->
<a href="${h.url_for(controller='person',
action='delete',
id=item.id)}"
onclick="return confirm('Are you sure you want to delete #${item.id}?');">[delete]</a></td>
</tr><tr>
% endfor
...
แล้วก็ไปเพิ่ม action ใน controller: person.py
def delete(self, id):
if id is None:
about(404)
corpus = model.Corpus.query.get(id)
if corpus is None:
abort(404)
elixir.session.delete(corpus)
elixir.session.flush()
h.flash("corpus #%s deleted" % id)
return redirect_to(action='index')
ป.ล. ตัวอย่างเขียนคร่าวๆ นะครับ อาจจะผิดบ้าง copy & paste เลยก็อาจจะเจ๊ง ขออภัยด้วย