0

I currently use revel as underlying webframework. My template/logic is very basic. I fetch a []*someObject from the database and want to display it in a table.

{{ if .objs}}
    {{ range .objs }}
    <tr>
      <td><span>{{ .Title }}</span></td>
      <td>x</td>
      <td>y</td>
      {{ $id := .Id}}
      <td><a href="{{url "ObjectController.ViewObj" .Id}}">View {{ $id }}</a></td>
    </tr>
    {{ end }}
{{ end }}

This however produces a "template runtime error, index out of range" with no further information. The Problem is the url part.

{{url "ObjectController.ViewObj"}}

Works. Without additional .Id it's perfectly fine, but since I want to pass the id into the url this is not an option.

The route is:

/lobby/view/:objid                  ObjectController.View

EDIT(FIXED): Apparently I forgot to add id as parameter:

func (c ObjectController) View(id int64) revel.Result

I did not know that it throws such an error, I would expect a more specific error.

Julius F
  • 3,434
  • 4
  • 29
  • 44

1 Answers1

0

You are probably missing a parameter to your route in your routes file.

As you are trying to get the URL with a parameter (Id), the route to ObjectController.ViewObj should look something like this:

GET /your/route/:id ObjectController.ViewObj

If you forget the :id then you will get an "index out of range" error when trying to reverse the URL.

Uraza
  • 556
  • 3
  • 17
  • That's how my route looks like: GET /lobby/view/:objid ObjectController.View – Julius F Apr 25 '14 at 15:34
  • Are you able to reverse the URL correctly without the id? It should print something like /lobby/view/. In addition, did you check if you can access /lobby/view/1 (or whatever your index is) in your browser, just to be sure that the controller works as expected? – Uraza Apr 25 '14 at 21:01