简体   繁体   中英

How to stop one specific user to delete his account in Ruby on Rails?

I have this code in /views/devise/registrations/edit.html.erb

<b>Cancel my account</b>
<%= link_to "Cancel my account", registration_path(resource_name), :data => { :confirm => "Are you sure?" }, :method => :delete %>.

which allows the logged in user to delete his account. Now I do NOT want one user with id=223 to have this permission/link. How can I do this ? I am not not sure the below cod in edit.html.erb file will work (or is there any better way to do it?)

EDIT - Ok this below code is working but how to do it through controller ?

<% if current_user.id != 223 %>
<b>Cancel my account</b>
    <%= link_to "Cancel my account", registration_path(resource_name), :data => { :confirm => "Are you sure?" }, :method => :delete %>.
<% end %>

Controller has nothing to do with a link being displayed or not. However you can disable the deletion in the controller by checking the same condition.

Anyways, you should create at least a model property for this. This id hardcode thingy is not nice, so hide it at least and make it not being repeated.

model User
  ...

  def can_delete_account?
    id != 223  # ugly hack
  end

end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM