Star Rating

344 views
Skip to first unread message

Michael Gheith

unread,
Feb 8, 2013, 5:52:19 PM2/8/13
to web...@googlegroups.com
Hello web2py community :)

I'm trying to implement a simple star rating for my site.  I followed the steps in the web2py Application Development Cookbook, but it is not working unfortunately; I select 2 stars for example, but it's not being saved to the database.

I have taken a look at the star rating widget in plugin_wiki, and could not get that to work either!

Can any of you provide me with a .w2p of something that works?


Hope to hear from any of you soon, thanks in advance!
Michael Joseph Gheith

Bruno Rocha

unread,
Feb 8, 2013, 6:33:24 PM2/8/13
to web...@googlegroups.com
https://github.com/mdipierro/web2py-recipes-source/blob/master/apps/04_advanced_forms/web2py.app.star_rating.w2p?raw=true

--
 
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Michael Gheith

unread,
Feb 9, 2013, 1:04:57 PM2/9/13
to web...@googlegroups.com
Hello rochacbruno,

I loaded this application on two different machines/environments, and the information in the form is not being saved to the database song table.  I tried this as both a logged in user, and not logged in user.

I began to trouble shoot, and modified the form from:
def index():
    form = SQLFORM(db.song)
    return dict(form = form)

to the following:
def index():
    form = SQLFORM(db.song)
    if form.process().accepted:
        response.flash = 'form accepted'
    elif form.errors:
        response.flash = 'form has errors'
    else:
        response.flash = 'please fill out the form'
    return dict(form = form)

It looks like the star rating value is not being sent from the form unfortunately because I get 'form has errors', 'value not allowed'.  Any ideas?


Best,
Michael Gheith

Michael Gheith

unread,
Feb 9, 2013, 1:49:59 PM2/9/13
to web...@googlegroups.com
I printed the request.vars to the console, and this is what I got after form submission:
<Storage {'_formkey': '7c32617c-2136-4d8d-ad54-ea7ae454e551', '_formname': 'song/create', 'title': 'Thriller'}>

So the star rating is definitely not making its way back to the server.  I'm guessing it should look like the following:
<Storage {'rating': '5', '_formkey': '7c32617c-2136-4d8d-ad54-ea7ae454e551', '_formname': 'song/create', 'title': 'Thriller'}>

Please help :)

Thanks,
Michael Gheith

Alan Etkin

unread,
Feb 10, 2013, 6:21:27 AM2/10/13
to web...@googlegroups.com
So the star rating is definitely not making its way back to the server.  I'm guessing it should look like the following:
<Storage {'rating': '5', '_formkey': '7c32617c-2136-4d8d-ad54-ea7ae454e551', '_formname': 'song/create', 'title': 'Thriller'}>

I have reproduced the issue with firefox 18.0.1 and the built-in server (Version 2.4.1-alpha.2+timestamp.2013.02.08.12.41.08).

The rating select value is not retrieved server-side, it is being skipped somehow. Not sure where (perhaps when creating the request object).

Alan Etkin

unread,
Feb 10, 2013, 9:12:49 AM2/10/13
to web...@googlegroups.com
> So the star rating is definitely not making its way back to the server.

Right. This seems not to be a web2py issue: the plugin is setting a disabled attribute for the rating input, so the field is omitted on processing. I found a workaround, adding this to the view:

{{=SCRIPT("""
jQuery("
form").submit(
  function(){
    jQuery("
[name=rating]").removeAttr("disabled");
  }
);
"""
)}}


Michael Gheith

unread,
Feb 10, 2013, 2:35:47 PM2/10/13
to web...@googlegroups.com
Hello Alan,

I added this code to the view and it is still not working.  Can you post your working .w2p somewhere?  It makes sense that the select field that represents that stars is somehow disabled.

Hope to hear from you soon Alan!
Best,
M.G.

Michael Gheith

unread,
Feb 10, 2013, 2:47:01 PM2/10/13
to web...@googlegroups.com
Here is the code that worked for me when added to the view.  Send all your thanks to Alan Etkin!!

<script>
$(document).ready(function() {

   $("form").submit(
     function() { 
     $("[name=rating]").removeAttr("disabled");
     }
   );
   
});
</script>




On Friday, February 8, 2013 4:52:19 PM UTC-6, Michael Gheith wrote:

Alan Etkin

unread,
Feb 11, 2013, 5:08:43 AM2/11/13
to web...@googlegroups.com
Here is the code that worked for me when added to the view. ...

Good catch, I should have added the .ready call to wait until the form element is available.

The rating plugin worked without this. We could fix the plugin script so there's no need for the workaround.
 

Ron Chatterjee

unread,
Feb 6, 2016, 3:01:43 PM2/6/16
to web2py-users
I have the view like this using plugin and still didn't work (insert values in the database). Any thoughts?


{{extend 'layout.html'}}

{{=form}}


<script>
$(document).ready(function() {
   $("form").submit(
     function() {
     $("[name=rating]").removeAttr("disabled");
     }
   );
  
});
</script>



Massimo Di Pierro

unread,
Feb 7, 2016, 12:45:36 AM2/7/16
to web2py-users
In the other thread you were missing form.process() in controller. ;-) could it be the same problem?

Ron Chatterjee

unread,
Feb 7, 2016, 10:41:28 AM2/7/16
to web2py-users
Yes. It works. My eyes fooled me as I copied and pasted from here:


The correction should be .process() in the controller. 

Sorry and Thank you again Massimo. You rock! 

Massimo Di Pierro

unread,
Feb 7, 2016, 11:22:34 AM2/7/16
to web2py-users
Thanks for checking the code! It helps us more than you think.
Reply all
Reply to author
Forward
0 new messages