Cameron Bass Cameron Bass - 2 months ago 14
Ajax Question

Rails Form helpers - Input - Ajax

I have a this form.

<%= form_for @snitch, html: { class: "form-actions", method: 'delete' } do |form| %>
<span class="button-text"><%= link_to 'NO WAY!', home_base_url_or_default(root_path), rel: "modal:close" %></span>
<input type="submit" class="button button--modal delete-snitch" data-snitch-id="<% @snitch.token %>" value="Yes, delete it.">
<% end %>


As you can see on the 3rd line there is an input type. I have this attribute on it
data-snitch-id
I have it set to the snitch token. I'm just wondering if that is correct because its coming back as undefined...

Here is how I know.

I'm using it for a Ajax call

$(document).on('click','.delete-snitch', function(e) {
e.preventDefault();
var snitchID = $(this).attr('data-snitch-id');
$.ajax({
type: 'DELETE',
url: '/snitches/' + snitchID,
datatype: 'json'
success: function(){
$('#tr-for-snitch-' + snitchID).fadeOut
}
});
});


But it's not working because the
data-snitch-id
comes back as undefined.

Answer

You have to use <%= @snitch.token %> instead of <% @snitch.token %> to output that value.

Comments