Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add a extra field in todo application
    primarykey
    data
    text
    <p>How to add a extra field in todo application created using Play framework using Scala? I am using anorm DB... I am getting an error named <code>"not found: value Task"</code> in <code>Application.scala</code> at line 24. I have tried it out, please point out my mistake. Thanks in advance!</p> <p><strong><code>task.scala</code>:</strong></p> <pre><code>package models import anorm._ import anorm.SqlParser._ import play.api.db._ import play.api.Play.current case class Task(id: Long, label: String, name: String) object Task { val task = { get[Long]("id") ~ get[String]("label") ~ get[String]("name") map { case label~name =&gt; Task(id, name) case id~label =&gt; Task(id, label) } } def all(): List[Task] = DB.withConnection { implicit c =&gt; SQL("select * from task").as(task *) } def create(task: Task): Unit= { DB.withConnection { implicit c =&gt; SQL("insert into task (label,name) values ({label},{name})").on( 'label -&gt; label, 'name -&gt; name ).executeUpdate() } } def delete(id: Long) { DB.withConnection { implicit c =&gt; SQL("delete from task where id = {id}").on( 'id -&gt; id ).executeUpdate() } } } </code></pre> <p><strong><code>application.scala</code> (controller class):</strong></p> <pre><code>package controllers import play.api._ import play.api.mvc._ import play.api.data._ import play.api.data.Forms._ import play.api.data.Form import play.api.data.Forms.{tuple,nonEmptyText} import play.api.mvc.{Action, Controller} import anorm.NotAssigned import models.Task object Application extends Controller { def index = Action { Redirect(routes.Application.tasks) } val taskForm = Form( tuple( "label" -&gt; nonEmptyText, "name" -&gt; nonEmptyText ) ) def tasks = Action { Ok(views.html.index(Task.all(), taskForm)) } def showTask= Action { Ok(views.html.test(Task.all(), taskForm)) } def newTask = Action { implicit request =&gt; taskForm.bindFromRequest.fold( errors =&gt; BadRequest(views.html.index(Task.all(), errors)), { case (label, name) =&gt; { Task.create(Task(NotAssigned, label, name)) Redirect(routes.Application.showTask) } } ) } def deleteTask(id: Long) = Action { Task.delete(id) Redirect(routes.Application.showTask) } } </code></pre> <p><strong>Index (view file):</strong></p> <pre><code>@(tasks: List[Task], taskForm: Form[(String, String)]) @import helper._ &lt;h2&gt;Add a new task&lt;/h2&gt; @form(routes.Application.newTask) { @inputText(taskForm("label")) @inputText(taskForm("name")) &lt;input type="submit" value="Create"&gt; } </code></pre> <p><strong><code>test.html</code> (view file 2):</strong></p> <pre><code>@(tasks: List[Task], taskForm: Form[(String,String)]) @import helper._ @main("Todo list") { &lt;h1&gt;@tasks.size task(s)&lt;/h1&gt; &lt;ul&gt; @tasks.map { task =&gt; &lt;li&gt; &lt;b&gt;@task.label&lt;/b&gt; &lt;b&gt;@task.name&lt;/b&gt; @form(routes.Application.deleteTask(task.id)) { &lt;input type="submit" value="Delete"&gt; } &lt;/li&gt; } &lt;/ul&gt; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload