Friday, 23 August 2013

Controller requires a value for one parameter - Internal server error 500

Controller requires a value for one parameter - Internal server error 500

Does implementing of AJAX with Symfony2 is so difficult as I see it now?
My goal is to implement a one-page application that displays a form for
creating posts (each post has a post title and a post content). When the
form is submitted, AJAX takes in charge displaying the post below the form
in the div#output.
My template looks like:
{% extends '::base.html.twig' %}
{% block body %}
<form action="{{ path('post_creates', { 'id': entity.id }) }}"
id="form_ajax" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}
<p>
<input type="submit" value="create" />
</p>
</form>
<ul class="record_actions">
<li>
<a href="{{ path('post') }}">
Back to the list
</a>
</li>
</ul>
<div id="output"></div>
{% endblock %}
{% block javascripts %}
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script
src="http://jwpsrv.com/library/leLjogZ6EeOBvCIACusDuQ.js"></script>
<script src="{{ asset('bundles/testblog/js/test2.js') }}"></script>
<script>
$("#form_ajax").submit(function(){
tinyMCE.get("test_bundle_blogbundle_posttype_postContent").save();
var content= $("#test_bundle_blogbundle_posttype_postContent").val();
var title = $("#test_bundle_blogbundle_posttype_postTitle").val();
$.ajax({
type: "POST",
url: "{{ path('post_creates', { 'id': entity.id }) }}",
data: {datas:title, date:content},
cache: false,
success: function(data){
$('#output').html(data);
}
});
return false;
});
</script>
{% endblock %}
The controller is:
public function createsAction(Request $request, $id)
{
$request = $this->container->get('request');
$entity = new Post();
if($request->isXmlHttpRequest())
{
$title = $request->request->get('datas');
$content = $request->request->get('date');
$em = $this->container->get('doctrine')->getEntityManager();
if($title != '')
{
$entity->setPostContent($content);
$entity->setPostTitle($title);
$id=$entity->getId();
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
$post = $em->getRepository('TESTBlogBundle:Post')->find($id);
}
else {
$post = $em->getRepository('TESTBlogBundle:Post')->find($id);
}
$deleteForm = $this->createDeleteForm($id);
return
$this->container->get('templating')->renderResponse('TESTBlogBundle:Post:show.html.twig',
array(
'entity' => $post, 'id' => $id,
'delete_form' => $deleteForm->createView(),
));
}
else {
return $this->indexAction();
}
}
I have a the following problem:
Controller requires that you provide a value for the "$id" argument
(because there is no default value or because there is a non optional
argument after this one).
When I var_dump the $id inside my controller, I always have null. My
controller works fine if I pass a value to it in function arguments. My
knowledge in Symfony2 doesnt allow me to find what I am missing. Your help
is really appreciated.

No comments:

Post a Comment