I'm trying to reproduce the example in the 'Voting and Rating' Ajax
Recipe from the official book (http://www.web2py.com/book/default/
chapter/10#Voting-and-Rating), but I simply cannot manage to get it
working. After inserting two images in the database, I try to vote
them and nothing happens. However, the error log contains the
following message:

File "/home/notnasiul/web2py/web2py/applications/voting/controllers/
default.py", line 10, in vote
    new_votes = item.votes + 1
AttributeError: 'NoneType' object has no attribute 'votes'

So it seems that either the query ( item = db.item[request.vars.id] )
is not being completed or there's nothing in request.vars.id. Just in
case, here's the whole code from the example:

# ------------------------ Model

db = DAL('sqlite://images.db')
db.define_table('item',
    Field('image', 'upload'),
    Field('votes', 'integer', default=0))

# ---------------------- Controller

def list_items():
    items = db().select(db.item.ALL, orderby=db.item.votes)
    return dict(items=items)

def download():
    return response.download(request, db)

def vote():
    item = db.item[request.vars.id]
    new_votes = item.votes + 1
    item.update_record(votes=new_votes)
    return str(new_votes)

# ----------------------- View

{{extend 'layout.html'}}

<form><input type="hidden" id="id" value="" /></form>
{{for item in items:}}
<p>
<img src="{{=URL('download', args=item.image)}}"
     width="200px" />
<br />
Votes=<span id="item{{=item.id}}">{{=item.votes}}</span>
[<span onclick="jQuery('#id').val('{{=item.id}}');
       ajax('vote', ['id'], 'item{{=item.id}}');">vote up</span>]
</p>
{{pass}}

I know there's a plugin for a nice rating system with stars, but I
just want this simple method instead! : ) What is going wrong? Why
doesn't the example work?

Reply via email to