Jquery rendering partially only once, the second time it does not appear again, just shows the previous one

I need to add partial jquery for my script. I cannot partially handle ajax, as it requires interaction with the controller, and I am limited to not use the controller.

So i do it

$('#button_add').click(function(){ $('#content_form_div').append(" <%= j render(:partial=> 'pages/content/call_text_phone', :locals => @locals.merge!(counter: cookies[:counter] + 1 ), :formats=>[:html]) %>"); }); 

The first time it renders partial, but the second time it actually does not partial.

Any idea to do this without using ajax.

+1
source share
2 answers

After spending time on my own decision, everything became crazy, as there is code spoiled and more lines of code.

For nested forms, I got this as a solution for me, since it also does not require interaction with the controller.

http://railscasts.com/episodes/196-nested-model-form-part-1

http://railscasts.com/episodes/197-nested-model-form-part-2

and one more thing, I added getRandomInt to jquery to create a unique identifier that is needed to avoid duplicate timestamps for fields instantly created without a time difference.

 function add_fields(link, association, content) { // I changed below line by adding getRandomInt method to assure unique id. var new_id = new Date().getTime() + getRandomInt(1, 5000); var regexp = new RegExp("new_" + association, "g") $(".all_fields").append(content.replace(regexp, new_id)); } function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } 
0
source

To reload something on the screen, you need to reload it (or recreate it in javascript), it seems to me that you are inserting static HTML and the script does what it says. It displays static HTML.

0
source

Source: https://habr.com/ru/post/944788/


All Articles