Search code examples
jqueryruby-on-railsviewpartial-viewsrenderpartial

jquery render partial just one time, for second time it not render again just shows previous one


I've to append partial by jquery for my scenario I can't render partial using ajax as it needs controller's interaction and I'm restricted to not use controller.

So I'm doing this

$('#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]) %>");
   });

It first time render partial but second time it does not actually render partial.

Any idea to do this without using ajax.


Solution

  • After spending time on my own solution, things got crazy as there is code messing up and more lines of code.

    For nested forms I got this as solution for me as it also not requires controller's interaction.

    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 in jquery to create unique id which is neccessary to avoid duplication of timestamp for fields created instantly with no 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;
    }