[web2py] Re: How to update db record using ajax?

2015-10-22 Thread Edward Shave
Thanks a bunch for your very detailed reply.
After reading your post I have managed to get a version working just the 
way I want.
Thanks again,
Ed

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: How to update db record using ajax?

2015-10-20 Thread Leonel Câmara
I usually have a controller like this:
 
   def update_or_insert_thing():
response.view ='generic.json'
ret = None
if request.vars:
if request.vars.id:
request.vars.id = int(request.vars.id)
ret = db(db.thing.id == request.vars.id).validate_and_update
(**request.vars)
else:
ret = db.thing.validate_and_insert(**request.vars)
if ret and ret.errors:
response.flash = T('Validation Errors')
return {'id': request.vars.id or ret.id, 'errors': ret.
errors}
return {'id': request.vars.id or ret.id, 'errors': []}
return {}


Then all you need is to post to it using your dict as the data. Something 
like this

$.ajax(
{
  url: "{{=URL('default', 'update_or_insert_thing')}}",
  method: 'POST',
  data: {"id":3, "firstName":"John", "lastName":"Doe"}
  dataType: 'json'
}
).done(function(data) {... // Here you should deal with validation 
errors

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.