1

Thanks for your help with this! I have many newsavedmaps, and each can have multiple waypoints. The waypoints table connects to the newsavedmaps id with a field, "newsavedmap_id." I am new to Rails (using Rails 2), and I'm having trouble with a destroy feature. Essentially, if a user gets rid of a newsavedmap, I also want to eliminate the waypoints.

newsavedmap.rb

    has_many :waypoints, :dependent => :destroy

waypoint.rb

    belongs_to :newsavedmap

newsavedmap controller (I think this is the problem)

    def destroy
    @newsavedmap = Newsavedmap.find(params[:id])
    @waypoint = Waypoint.find(params[:id])
    @newsavedmap.destroy
    @waypoint.destroy

    respond_to do |format|
    format.html { redirect_to "/CODE" }
    format.xml  { head :ok }
    end
    end

I also have a view, newmaps.html.erb. A user sees his newsavedmap on the page, and he can click a link:

    <a href="/newsavedmaps/<%= newsavedmap.id %>" class="newremovemap"></a>

When he does, this is the javascript that kicks in:

    $('.newremovemap').click(function(){
        if (confirm('Are you sure you want to remove this map?')) {
            var mappage = $(this).closest('.wholemap'); 
            var map = $(this).parent();
            $.post(this.href, { _method: "delete", authenticity_token: $('#auth_token').val() }, function(){
                mappage.fadeOut();

            });
        };
        return false;
    })

Now, when I click the link, the alert message repeats many times, but the records are not removed. The error code is ActiveRecord::RecordNotFound (Couldn't find Waypoint without an ID). I've tried applying links like this to the problem ( Rails dependent destroy error), but I can't seem to find a solution.

Community
  • 1
  • 1
KDP
  • 634
  • 1
  • 11
  • 31

2 Answers2

2

you need to just destroy the newsavedmap, and associated waypoints will be automatically deleted.

  def destroy
    @newsavedmap = Newsavedmap.find(params[:id])
    @newsavedmap.destroy

    respond_to do |format|
      format.html { redirect_to "/CODE" }
      format.xml  { head :ok }
    end
  end

the other most important thing is that with a single params[:id], you are trying to find objects of two classes(Newsavedmap,Waypoint), that is wrong. as per the code link is for Newsavedmap and hence you can find its object by params[:id], not of Waypoint.

One more thing, that is important that you are trying to call a javascript function on that click, which is redirecting to newsavedmap show page.Try to change that link as well :

<%= link_to 'Destroy', newsavedmap_path(@newsavedmap),
        :confirm => 'Are you sure?', :method => :delete %>
Sachin Singh
  • 7,107
  • 6
  • 40
  • 80
  • Sachin, this answer gets me 90% of the way there (thanks!) The records are being deleted. The only remaining issue is that after I click to delete, I receive the alert message multiple times: once for the newsavedmap, and one time for each waypoint associated with the map. Is there a way to only have it appear once and to let the user just confirm the action once? FYI, I'm running into issues with the link_to approach, but I'll work on that later. For now, I'm just using the javascript approach. – KDP Aug 18 '13 at 22:32
  • OK, got it, I went with <%= link_to 'Delete', newsavedmap_path(newsavedmap.id), :confirm => 'Are you sure?', :method => :delete, :class=> 'deletesavedmapbutton' %>. I left out the javascripting. That eliminated the repeated alerts. – KDP Aug 19 '13 at 20:59
1

If you have added the dependent destroy in newsavedmap you don't need to call

@waypoint.destroy // not needed

Arihant Godha
  • 2,339
  • 2
  • 26
  • 50