Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to write a controller in Symfony2 to edit content with Jeditable
    primarykey
    data
    text
    <p>I'm using jeditable plugin for JavaScript and I want to implement it in my Symfony2 project. I want to edit a name with the plugin and that name to be edited in the database, too, not the change to be gone when I refresh the page, but in my case, it's gone. :(</p> <p>I'm almost sure that the controller shouldn't be in that way and the problem is from it, but how exactly to write it? Here it is:</p> <pre><code>public function editCategoryAction(Request $request, $id) { $category = $this-&gt;repository-&gt;find($id); $form = $this-&gt;createForm(new CategoryType(), $category); if ($request-&gt;isMethod('POST')) { $form-&gt;bind($request); if ($form-&gt;isValid()) { $this-&gt;em-&gt;persist($category); $this-&gt;em-&gt;flush(); return $this-&gt;redirect($this-&gt;generateUrl('categories')); } } return $this-&gt;render( 'AcmeBudgetTrackerBundle:Categories:categories.html.twig', array( 'form' =&gt; $form-&gt;createView())); } </code></pre> <p>This is my <strong>template</strong>:</p> <pre><code> &lt;a href="{{ path('edit_category', { 'id': cat.id}) }}"&gt; &lt;strong class="edit"&gt; {{ cat.name }} &lt;/strong&gt; &lt;/a&gt; &lt;script&gt; var token = "{{form._token.vars.value}}"; var path = "{{ path('edit_category', { 'id': cat.id}) }}"; &lt;/script&gt; </code></pre> <p>And this is in the <strong>.js file</strong>:</p> <pre><code>(function(){ $('.edit').editable(function (value, settings) { var data = {}; data[this.id] = value; data["_token"] = token; console.log(path); console.log(data); $.post(path, data); return(value); }, { indicator:'Saving...' }); }) (); </code></pre> <p>The output in the console looks fine:</p> <pre><code>/BudgetTracker/web/app_dev.php/edit_category/52 Object {: "Edu", _token: "9d29860b59ccafbc265ea12346c91fa7e378cc97"} </code></pre> <p>but the problem is that nothing is posted to the database and when I hit refresh the change I made is gone.</p> <p>Can you please help me to solve this? Thanks in advance! :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload