Edit using ajax

1–2 minutes

read

Edit using ajax

 

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 "Edit", edit_question_path(question.id),method: :get, :remote => true%>

Step 2: Create a file, edit.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}); 
$("#popup5").html('<%= escape_javascript(render 'questions/form') %>');

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 rendering.

Step 3: Add format.js to your controller (app/controllers/question_controller.rb). Your destroy method should now look like this:

def edit

@question = Question.find(params[:id])
 respond_to do |format|
 format.js
 end

And now you are done.

Hope it helps.

 

Leave a comment