jquery - Inline editing with Laravel 5 and Bootstrap Editable: 405 Method Not Allowed -
i have comment form , list on each post in app, imported bootstrap editable able give users access edit comments inline without page reload.
but keep getting error
methodnotallowedhttpexception in routecollection.php line 219:
post http://localhost/r2/public/posts/comment/update 405 (method not allowed)
i'm assuming it's comment routes can't figure out what.
edit: after adding type: 'post'
ajaxoptions
started getting different error
creating default object empty value
it seems input::get('commenter_comment')
not returning anything. guess wrong anyway not x-editable field appearing.
how can grab x-editable field?
routes
route::get('{post}/comment', ['as' => 'comment', 'uses' => 'commentcontroller@index']); route::post('{post}/post_this_comment', 'commentcontroller@post_this_comment'); route::get('{post}/recaptcha', 'commentcontroller@recaptcha'); route::get('{post}/reply_comment', 'commentcontroller@reply_comment'); route::post('{post}/per_page', 'commentcontroller@per_page'); route::post('{post}/comment/update', 'commentcontroller@update');
update()
method in commentcontroller
public function update() { $commentid = input::get('pk'); $newcomment = input::get('commenter_comment'); $commentdata = comment::whereid($commentid)->first(); $commentdata->comment = $newcomment; if($commentdata->save()) return response::json(array('status'=>1)); else return response::json(array('status'=>0)); }
the view
<script type="text/javascript"> $(document).ready( function(){ $.ajaxsetup({ headers: { 'x-csrf-token': $('meta[name="csrf-token"]').attr('content') } }); $('.testedit').editable({ validate: function(value) { if($.trim(value) == '') return 'value required.'; }, type: 'textarea', url: 'comment', title: 'edit comment', placement: 'top', send:'always', ajaxoptions: { datatype: 'json' } }); } ); </script> <p><a href="#" class="testedit" pk="{{ $each_comment->id }}">{!! $each_comment->comment !!}</a></p>
as mentioned, methodnotallowedhttpexception
thrown when using different http verb. mean, in routes declare update route post
verb:
route::post('{post}/comment/update', 'commentcontroller@update');
however, in ajax options when type not defined request in get, must define it:
ajaxoptions: type: 'post'
now, error comes. not passing post id expected in route's definition, that's why receiving empty id, in consequence, empty object returned database. so, must pass as. first, lets change markup little bit each comment in order set the post's url via data-url
, pk via data-pk
attributes:
<a href="#" class="testedit" data-pk="{{ $each_comment->id }}" data-url="{!! url($post->id . '/comment/update') !!}"> {!! $each_comment->comment !!} </a>
now, x-editable
catch url
, pk
values automatically without set explicitly. final code should close this:
$(document).ready( function(){ $.ajaxsetup({ headers: { 'x-csrf-token': $('meta[name="csrf-token"]').attr('content') } }); $('.testedit').editable({ validate: function(value) { if($.trim(value) == '') return 'value required.'; }, type: 'textarea', title: 'edit comment', placement: 'top', send:'always', ajaxoptions: { datatype: 'json', type: 'post' } }); } );
don't forget every argument defined in routes injected in controller's function. example, route defining {post}
expected post id edit:
route::post('{post}/comment/update', 'commentcontroller@update');
so, in update()
inject argument:
public function update(request $request, $post) { //$post has id of post edited // catching pk id: $pk = $request->get('pk'); // catching new comment $comment = $request->get('value');
Comments
Post a Comment