dsu1/Archivist

Implement Deleting User Accounts

Closed this issue · 1 comments

dsu1 commented

Give users the option to destroy their accounts.

Tasks:

  • Update Users Destroy action
  • Add a user account destroy button on the user's home page
    • Presumably use Ajax and send the request in JSON format
  • Make sure all the archives and contents belonging to that user are removed
dsu1 commented

Users Controller

def destroy
	@user = User.find(user_id_params[:id])
	if current_user.id == @user.id
		@user.destroy
		session[:user_id] = nil
		redirect_to root_url, :notice => "Your Account has been deleted"
	end
end
  • Renamed the action from delete to destroy.
  • Find the user based on the id params.
  • Check that it matches the logged in user's id
    • Then destroy the user, clear their session id, and redirect them to the root sign up page with a message that their account was successfully destroyed.

views/users/show.html.erb

<% if current_user %>
	<h3> Welcome <%= current_user.username %>!</h3>
	<% if @archives.nil? or @archives.empty? %>
		<p>You don't have any archives.</p>
		<%= link_to  "Create An Archive", new_archive_path %><br />
	<% else %>
		<%= render partial: "archives/archives", collection: @archives %>
	<% end %>
	<br />
	<%= link_to "Delete This Account", user_path(@user), :class => "button", :method => :delete, data: {confirm: 'Are you sure?'} %>
<% else %>
  • Added a header welcoming the user to their home page
  • I made a change to redirect signing up users to their home page. The home page checks if they have archives and but the archive instance variable isn't instantiated when redirected in this way, so I added a check for nil since it's not instantiated. The code was improper and should have checked if the user's archive collection was empty which I added.
  • Added a link to new archives in the event the user doesn't have any archives
  • I added a delete user account link which requests the destroy action I made

views/archives/index.html.erb

<% if @archives.empty? %>
	<p>You don't have any archives</p><br />
	<%= link_to "Click here to create a new archive", new_archive_path, :class => "button" %>
<% else %>
	<%= render partial: "archives", collection: @archives %>
<% end %>
  • Added a link to new archives in the event the user doesn't have any archives

Sessions Controller

redirect_to user_path(user.id), :notice => "Logged in"

  • Made a change to sessions. What was happening is that after properly signing up, the user would be redirected to the sign up page or the server would crash so I fixed this redirection by sending the user to their home page.