french_dev
french_dev

Reputation: 2177

FOSJsRouting bundle returns error No route found for "GET

I have this table in my twig view:

<table>
  <thead>
    <tr>
      <th>Data 1</th>
      <th>Data 2</th>
      <th>DETAILS</th>
      <th>EDIT</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    {% for pc in arrayPointComptage %}
      <tr>
        <td>{{ pc.data1}}</td>
        <td>{{ pc.data2}}</td>
        <td>
          <a><button class="btn btn-info btn-xs showDetail" href="{{ path('detailsPointsComptage', {'id': pc.id }) }}">Detail</button></a>
          <a href="{{ path('editPointsComptage', {'id': pc.id }) }}"><button class="btn btn-warning btn-xs">Edit</button></a>
        </td>
      </tr>
    {% endfor %}
  </tbody>
</table>

<dialog id="window" title="detail pc"></dialog>

I am using FOSJsRoutingBundle, in order to use route with ajax method.

When I click on the detail button, it returns me in a dialog tag with all detail I need.

This is the javascript code in order to retuns me the details in a dialog window:

// dialog window behavior
(function() {
    var dialog = document.getElementById('window');
    // Array of the buttons.
    var showButtons = document.getElementsByClassName('showDetail');
    // Event handler
    var showDialog = function() {
      // Now you have to use the show button as base , to find the data you want to display...
      console.log(this);
      dialog.show();
    };
    var i, len = showButtons.length;
    for(i = 0; i < len; ++i) {
        showButtons[i].onclick = showDialog;
    }
})();

//jax with FOSJsRouting
  $('.showDetail').click(function() {
    $.ajax({
      type: "GET",
      'url': Routing.generate('detailsPointsComptage', {'id': $(this).val()}),
      'success': function(loadDetail) {
          $('#window').html(loadDetail);
      }
    });
  });

Following the documentation, I expose my route to true argument in app/config/config.yml:

fos_js_routing:
    routes_to_expose: [ detailsPointsComptage ]

And to be sure in my routing.yml:

detailsPointsComptage:
    path:  /my/path/to/detail/{id}
    defaults: { _controller: MyBundle:MyController:detailsPointsComptage }
    requirements:
    methods: GET
    options:
        expose: true

When i click on the detail button, I have this error:

No route found for "GET /my/path/to/detail"

In the browser, with right click I can inspect all element. On the console tab I have this:

<button class=​"btn btn-info btn-xs showDetail" href=​"/​symfony_app/​web/​my/​path/to/​detail/​1">​Detail​</button>​ http://localhost/symfony/web/my/path/to/detail Failed to load resource: the server responded with a status of 404 (Not Found)

Indeed, if I follow the link http://localhost/symfony/web/my/path/to/detail, I have the error of route not found for GET, but if I write this url http://localhost/symfony/web/my/path/to/detail/1 it returns me the right view.

Where am I wrong?

Upvotes: 0

Views: 973

Answers (1)

tchap
tchap

Reputation: 3432

Your route needs the id parameter, it is required, so this behaviour is normal.

The problem is that your $(this).val() doesn't return the id as you expect, you should do something like this instead, since the route is already generated in the href attribute of your button :

$('.showDetail').click(function() {
    $.ajax({
      type: "GET",
      'url': $(this).attr('href'),
      'success': function(loadDetail) {
          $('#window').html(loadDetail);
      }
    });
    return false;
  });

NB : using href for a <button> doesn't really make any sense, you should use a <a> (or use another attribute like data-link for instance) :

<a class="btn btn-info btn-xs showDetail" href="{{ path('detailsPointsComptage', {'id': pc.id }) }}">Detail</a>

Upvotes: 1

Related Questions