Nested insert_html error: Ruby On Rails & Javascript

Hello everybody! This problem is driving me crazy! This seems like a bug in the Rails JS rendering, but I'm not sure.

Short review:

I am trying to create a RELEASE with has_many TRACK. These TRACKS have many TRACK_LINKS. Then these TRACK_LINK associated TRACK_LINK_TYPE (mp3, m3u, podcast, whatever). Think of it as an album. Therefore, adding a release, you can add album tracks, and each album track can have different links (mp3, m3u, podcast, whatever). I am at a very early stage and use scaffolding - the user can create a release in the form, add tracks through JS and add track links through JS. All this works great, but adding tracks does not work when I have the "Add Track Link" feature in the code! It silently fails - there is no error, it just goes to # anchor.

In the main release form, I do a partial TRACK and add a link to add TRACK:

<div id="tracks">
    <%= render :partial => 'track', :collection => @release.tracks -%>
</div>

<%= add_track -%>

Now, in the TRACK part, I partially pass TRACK_LINK and add a link to add TRACK_LINKs:

<div class="track">
    <fieldset>
    <legend>Track</legend>
    <p> 
        <% @track = track %>
    <%= error_messages_for :track -%>

        <% fields_for_track(track) do |track_form| -%>
        Track Name
    <br />
    <%= track_form.text_field :name -%>
    <%= link_to_function "remove", "$(this).up('.track').remove()" -%>
    <% track_links_id = "track_links_#{Time.now.to_i.to_s}" %>
    <div id="<%= track_links_id %>">
            <%= render :partial => 'track_link', :collection => @track.track_links -%>              
    </div>
    <%= add_track_link track_links_id -%>   
    <% end -%>
    </p>
</fieldset>
</div>

And in partial TRACK_LINK I create TRACK_LINK fields (with my TRACK_LINK_TYPE field):

<div class="track_link">
    <% @track_link = track_link -%>
<% fields_for_track_link(track_link) do |track_link_form| -%>
    Track Link
    <br />
    <%= track_link_form.text_field :url -%>
    <%= select ("track_link_type", "id", @track_link_type.map {|type| [type.name, type.id]}) -%>        
        <%= link_to_function "remove", "$(this).up('.track_link').remove()" -%>     
<% end -%>
</div>

Finally, here are the helper methods of link_to_function:

module ReleasesHelper
    def fields_for_track(track, &block)
        prefix = track.new_record? ? 'new' : 'existing'
        fields_for("release[#{prefix}_track_attributes][]", track, &block)
    end

    def fields_for_track_link(track_link, &block)
        prefix = track_link.new_record? ? 'new' : 'existing'
        fields_for("track[#{prefix}_track_link_attributes][]", track_link, &block)
    end

    def add_track
        track = Track.new
        track.track_links.build
        link_to_function 'Add Track' do |page| 
            page.insert_html :bottom, :tracks, :partial => 'track', :object => track
    end
end

    def add_track_link(name)
        link_to_function "Add Track Link" do |page|       
            page.insert_html :bottom, name, :partial => 'track_link', :object => TrackLink.new
    end
end
end

The result is that each link_to_f function works fine, but when I have the Add Track and Add Track Link functions at the same time, the Add Track link link_to_function does not work. Something about this, creating the Add Track Link feature is useless! Sorry for the long ass! I hope this is not annoying, but can someone lend a hand?

better praveen

+3
source share
1 answer
+1

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


All Articles