0

I am using symfony and twig and trying to route to a controller function that exists, using ajax. The route I am trying to get to seems to be appended to the current route (page) that is calling the ajax. What is causing this and what am I doing wrong? I am intermediate at this. Thanks in advance for your efforts.

The ajax looks like;

    $.ajax({url: "{{ path('material-stock_check') }}/" + quoteRing.materialId + "/" + quoteRing.gaugeId + "/" + decimal, success: function (results) {
            if (results.length === 0) {
                quoteRing.findStripWidthAlternates();

            }
        }});

and the controller looks like

    /**
 * Check if the strip width is in the Inventory
 * @Route("/check/{materialId}/{gaugeId}/{decimal}", defaults={"materialId" = 0, "gaugeId" = 0, "decimal" = 0}, name="material-stock_check")
 * @Method("GET")
 */
public function checkStripWidthAction (Request $request, $materialId, $gaugeId, $decimal)
{
    $em = $this->getDoctrine()->getManager();

    $materialStocks = $em->getRepository('UniflyteBundle:MaterialStock')->findAllByParams(['widthDecimal' => $decimal, 'materialId' => $materialId, 'gaugeId' => $gaugeId]);

    if ($request->isXmlHttpRequest()) {
        if (null === $materialStocks) {
            return new JsonResponse('failure');
        }
        $results = [];
        foreach ($materialStocks as $result) {
            $results[] = [
              'gaugeId'    => $result->getGauge()->getId(),
              'materialId' => $result->getMaterial()->getId()
            ];
        }

        return new JsonResponse($results);
    }
}

When the ajax is called I am getting

No route found for "GET /uniflyte/quote-ring/new/%7B%7B%20path('material-stock_check')%20%7D%7D/93/347/3.45" (from "http://localhost:8088/uniflyte/quote-ring/new/rolled-ring")

The ajax route looks appended to the existing route. What am I doing wrong?

2
  • 2
    It looks like {{ path(...) }} is not being evaluated by twig. Are you sure this code is within a Twig template. Are you sure it's processed like that? Could you post the code of controller where it's rendered? Commented Jun 6, 2018 at 20:08
  • 1
    you are correct with this Commented Jun 7, 2018 at 14:30

2 Answers 2

3

It seems {{ path(...) }} is not being evaluated by twig as @Omar Alves told.

try this, declare a variable in your twig file

<script>
    var url = '{{ path("material-stock_check") }}';
</script>

and then use it

Sign up to request clarification or add additional context in comments.

Comments

-1

Have you declare the path in route file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.