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.