Reputation: 691
I'm using AJAX first time and I have following problem
I have description and below I have field with button where I can input new description, after click on button I'd like to refresh only description using AJAX.
//_description.html.erb
<div class="description-field">
<%= @description.text %>
</div>
In my controller in update I set format.js
//update.js.erb
$('.description-field').replaceWith("<%= @description.text %>");
and
//_description.html.erb
<%= simple_form_for @description, remote: true do |f| %>
<%= f.input :text %>
<%= f.button :submit %>
<% end %>
But unfortunatelly, it refreshes description only once, when I click button second time there is no change, I had to refresh site to see new text. Could someone help me fix it?
Upvotes: 0
Views: 107
Reputation: 616
This is because .replacewith
replaces the entire html element, not just the content it contains. Change $('.description-field').replaceWith("<%= @description.text %>");
to $('.description-field').html("<%= escape_javascript @description.text %>");
Also use escape_javascript
as I did just to be safe.
Upvotes: 1