sam0101 sam0101 - 11 months ago 56
Ajax Question

js.erb template rendered as text

I am trying to make my destroy action work with ajax so I the page dont have to refresh when deleting images..the problem is when I try this with ajax it remove the image but instead it display this text:

<%= j render @category.images is my destroy action :

def destroy
@image = @category.images.find(params[:id])
respond_to do |format|
format.html { redirect_to category_path(@category) }

destroy.js.erb file:

$('#panel_<%= %>').html("<%= j render @category.images %>");

and here is where my delete link(i added remote: true as well):

<% @category.images.each do |image| %>
<div class="col-md-6">

<div class="panel panel-default panel-show" id= "panel_<%= %>">
<div class="panel-body panel-body-show">
<%= image_tag(, class: "img-in img-responsive") %>
<%= link_to "delete", category_image_path(@category, image), remote: true, method: :delete, data: {confirm: "Are you sure?"} %> <%= link_to "edit", edit_category_image_path(@category, image) %>
<ul class="imgs-tables">
<li><h2><%= image.i_name %></h2></li>
<hr class="for-title">
<li class="second-one"><p><%= image.i_bio %></p></li>
<% if image.price %>
<li class="price-title"><h3> Price: </h3> </li> <li class="price-li"><h4 class="for-price"><%= image.price %><i class="fa fa-ils"></i></h4></li><br/>
<hr class="price-line-up">
<hr class="price-line-down">
<% end %>
<ul class="sub-images-show">
<% image.sub_images.each do |sub_image| %>
<li><%= image_tag(sub_image.small_image.url(:small)) %></li>
<% end %>
<% end %>

and her is an image that show you what where the text is render

<blockquote class="imgur-embed-pub" lang="en" data-id="a/G9FVD"><a href="//"></a></blockquote><script async src="//" charset="utf-8"></script>

Answer Source

try this

destroy.js.erb file:

$('#panel_<%= %>').remove();