0

I have a nested form, which is loaded by add more button, In the nested form I have radio button. According to the radio button, a javascript function is called to hide or show a div. But for more than two or more form the radio button javascript calls the first div as div id is static. plz help. here the the code.

form.html.erb

 <%= f.fields_for :book_chapters  do |book| %>
 <%= render '/publications/book_chapter', :ff => book %>
 <% end %>
 <%= f.link_to_add "Add chapter", :book_chapters, :class => "add_category_link"%>

_book_chapter.html.erb

<%= ff.input :chapter_no %>
<%= ff.input :chapter_title %>
<%= ff.input :description %>
<%= ff.input :pdf, :hint => "Please upload Pdf's Only." %>     
<div class="control-group string optional">
  <label class="string optional control-label">Plan</label>
<div class="controls">
<%= ff.radio_button :plan, "free", :class=>"relat__atu relat__atu_no" %> Free
<%= ff.radio_button :plan, "premium", :class=>"relat__atu relat__atu_yes" %>Premium

<script>
  jQuery(".relat__atu").on("change", function(){
    jQuery("#esatu").toggle($(this).hasClass("relat__atu_yes"));
  })
</script>

<div id="esatu" style="display:none">
  <label for="relat_tipus_atu">
    <%= ff.input :credits %>
  </label>
</div>

<div class="controls">
  <%= ff.link_to_remove "remove", :class => "remove_category" %>
</div>

2 Answers 2

0

You can render the partial inside a div like

<div class='some_div'>
  <%= render '/publications/book_chapter', :ff => book %>
</div>

Change id of your div to class

<div class="esatu" style="display:none">

And change js to

jQuery(".some_div").on("change", ".relat__atu", function() {
  jQuery(".esatu", $(this).parent('.some_div')).toggle($(this).hasClass("relat__atu_yes"));
})
1
  • Made few more changes. Can you check now?
    – user946611
    Commented May 30, 2013 at 11:51
0

I managed to solve this problem by looking into nested form gem . Inside nested gem unique Id is generated for each addition dynamically. data-blueprint is parsed and "new_#{association}" is replaced with generated unique ID.In my case relation was like 'publication' has_many 'book_chapters' so my association was book_chapters. I assigned new row with id = "book_chapters" and its replaced by unique Id when new row is added.

Maybe its useful some one else

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.