So i have my destroy command. The problem is I don't want my users to be able to delete this category if it contains items. So I have an if statement in my destroy method. That works all great, but I'm using AJAX to have the page not load and using a destroy.js.erb file. My method is below:
def destroy
@promotion_type = PromotionType.find(params[:id])
if Promo.find_by_promotion_type_id(params[:id]).nil?
@promotion_type.destroy
respond_to do |format|
format.html { redirect_to(promotion_types_url) }
format.js {render :layout => false }
format.xml { head :ok }
end
else
respond_to do |format|
format.js {}
end
end
end
This is my javascript(jquery):
$('a.delete').click (function(){
if(confirm("Are you sure?")){
var row = $(this).closest("tr").get(0);
$.post(this.href, {_method:'delete'}, null, "script");
$(row).hide();
return false;
} else {
//they clicked no.
return false;
}
One of my problems is that once they click yes, it hides the row and AJAX deletes its. If I didn't hide it, it would be deleted but since the page is doesn't refresh, it looks like it wasn't deleted, so I hide it for the effect. The only problem with this solution is when I get to my categories. I don't want users to delete it if it has items inside it. I can't throw ruby code in my javascript file, and I can't (or don't know of a way) to throw javascript variables or $(this) into a .js.erb file.
The code runs correctly for the most part, but it hides the row now even if it failed to delete it. So i was going to throw an alert('Cannot be delete'); into my .js.erb file, but that alert fires regardless of whether it was or wasn't deleted. I'm sure there's a better solution to do all this, but I don't know it.