Next → Sixth Step, open and close tasks Fourth Step, C, like Controller ← Prev

Next → Sixth Step, open and close tasks Fourth Step, C, like Controller ← Prev

Fifth Step, getting dynamic

We set out to build the ultimate to-do list, but there are still some things missing. First off, we want to add new tasks, so let's get that done.

Add a link on the view/index.xhtml like this:

<h1>TodoList</h1>
<a href="/new">New Task</a>

Open a new file view/new.xhtml with a form to add a new task.

<html>
  <head>
    <title>TodoList</title>
  </head>
  <body>
    <h1>New Task</h1>
    <a href="/">Back to TodoList</a>
    <form method="POST" action="create">
      Task: <input type="text" name="title" /><br />
      <input type="submit" />
    </form>
  </body>
</html>

We will not need a method for this on our controller, in fact, actions can consist of either method and template or only one of them. The Controller can act as a View and the View can work like you may know it from PHP.

If you try to use this form you will notice that we have not yet defined a way to actually create the task.

You will get the default Ramaze error-page instead. Please take your time to explore it and see how Ramaze reacted on the error.

It will show you the back trace and what state the application is in at the moment, the request and response and the contents of the session. This is very useful for debugging and development, you can provide your own set of error-pages before going into production (or deactivate them fully) though.

OK, let's implement the action for #create, all we want to do is take the requests parameters and create a new task for it, this looks like following on your MainController.

def create
  title = request['title']
  TodoList[title] = {:done => false}
  redirect Rs()
end

That's all folks!

We get the title from the request-object, put it into our TodoList as undone and redirect back to the mapping of the current Controller ('/' in this case).

Now you can create as many tasks as you want, please don't get overworked ;)

Next → Sixth Step, open and close tasks Fourth Step, C, like Controller ← Prev

 
Back to top
tutorials/todolist-5.txt · Last modified: 2008/01/31 03:26 by reacher