How to add Ajax functionality to a form for replacing a DOM element with a partial?

roberthopman

Robert

Posted on November 13, 2020

How to add Ajax functionality to a form for replacing a DOM element with a partial?

How to add Ajax functionality to a form for replacing a DOM element with a partial?

Add to form remote: true or refactor to form_with

In your controller action, e.g. foobar, add:

def foobar

  respond_to do | format |
    format.js
    format.html
  end

end
Enter fullscreen mode Exit fullscreen mode

Create the same file name as the controller action:

foobar.js.erb

Content of foobar.js.erb:

$("#id").replaceWith("<%= j render('partial_name') %>");

Perhaps possible to extract partial from current view

_partial_name.html.erb

Add this back in the view with

<%= render "partial_name" %>

Done.

💖 💪 🙅 🚩
roberthopman
Robert

Posted on November 13, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related