Nick Price
Nick Price

Reputation: 963

Symfony2/Javascript delete alert

the main part of this question relates to how I can define a route in a javascript file. At the same time, I just want to make sure what I am doing is ok. Essentially, on my view page, I can see my records. When I output the records, each row I give

<input type="button" value="Delete" onclick="delete_alert( {{ alert[0].id }} )"/>

This gives the user the option to delete an alert. Before going straight into things, I like to first send it to javascript.

function delete_alert(id){
    var answer = confirm("Confirm delete");
    if (answer){
        $.get("NickAlertBundle_delete", { row: id });
    }
}

So if the delete is confirmed, it calls this route

NickAlertBundle_delete:
    pattern:  /view-alerts
    defaults: { _controller: NickAlertBundle:Alert:delete }
    requirements:
       _method:  GET

My first question here is they are deleting the alert from the view-alerts page. Once they confirm deletion, I dont want them to go anywhere, just have the view-alerts page refresh (as this will remove the deleted alert). But is giving the delete route a pattern of view-alerts confusing or wrong to do? Its what I want to do as I want them to stay on this page, just doesnt feel right.

Anyway, my real problem is the error

No route found for GET /NickAlertBundle_delete (from http://localhost:8000/view-alerts;) (404 Not Found)

So I dont think the way I have define the route in my javascript file is correct. So how can I fix this route? Thanks

Upvotes: 0

Views: 248

Answers (1)

Adib Aroui
Adib Aroui

Reputation: 5057

I think you should use an URL as a first parameter to jQuery get. You are currently using the Symfony2 route name, not the URL.

Change it to /alert-views.

OR:

You can add a data attribute to your HTML button that would contain your path, If you are using Twig as templating engine, you can write something like:

data-url={{path('NickAlertBundle_delete')}}

Then using the attr() jQuery function onclick on your button:

var url = $(this).attr('data-url');

Finally, the jQuery get function can be sent to url.

I hope it helps.

Upvotes: 2

Related Questions