Reputation: 1825
I'm using devise gem and showing user's objects on registrations/edit view.
I have such problem: when I'm clicking delete object link - it destroy user record, but should delete object.
User has many websites, so I show user his websited and want user available to delete it using this code:
<% @websites.each do |website| %>
<%unless current_user.websites.empty? %>
<%= link_to 'X', website_path(website), method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
<% end %>
Controller code:
class RegistrationsController < Devise::RegistrationsController
def edit
@websites = current_user.websites
@reports = current_user.financial_reports
end
end
class WebsitesController < ApplicationController
def destroy
@website = Website.find(params[:id])
if (current_user.id != @website.user_id)
redirect_to root_path
flash[:notice] = 'You are not owner!'
else
@website.destroy
respond_to do |format|
format.html { redirect_to websites_url }
format.js
end
end
end
I didn't change anything in standard behavior in Websites_controller.
Can someone suggest how to solve it ?
Upvotes: 0
Views: 48
Reputation: 34784
It would be worth checking your associations. If you have :dependent => :destroy
on the wrong end of the association between users and websites then destroying the website would cause the associated user to be destroyed too.
Upvotes: 2