Delete using ajax and fade out
Step 1: In your index.html.erb Add
:remote => true, :class => 'class_name' in
app/views/controller_name/index.html.erb.
Like this,
<%= link_to "Delete", question_path(question.id),method: :delete ,data: { confirm: "Are you sure?"}, :remote => true, :class => "delete_question"%>
Step 2: Create a file, destroy.js.erb, put it inside folder .erb files (under app/views/controller_name). It should look like this:
console.log("here") noty({text: 'Question is successfully destroyed!!', type: 'success', timeout: 1500, theme: 'defaultTheme',maxVisible: 5,template: '
',progressBar: true});
$(function() {
$('#die_<%=@question.id%>.question').fadeOut(1000);
});
now , if you got the above code its good else I m expaining too.
So, console.log (“here”) is just to check if we are redirected to right page or in simple words wheather ajax is working or not.
noty is just a notification for this you have to install noty.
If you dont able to install ,I will definetly write in other blog till then delete whole noty statement.
here comes the main part i.e. the function
a Simple function in which I am passing the id of class which is to unique so i have added the id of each question like here so as to make it unique.
Step 3: Add format.js { render :layout => false } to your controller (app/controllers/question_controller.rb). Your destroy method should now look like this:
def destroy
@question.destroy
respond_to do |format|
format.html { redirect_to '/', notice: 'Question was successfully destroyed.' }
format.json { head :no_content }
format.js { render :layout => false }
end
end
And now you are done.
Hope it helps.

Leave a comment