mardi 28 février 2017

Remove users from database (Ruby on Rails, Devise)

I'm trying to create a button to remove users from the database.

In the routes I have:

  devise_for :users, :controllers => { registrations: 'registrations' }
  match 'users/:id' => 'registrations#destroy', :via => :delete, :as => :admin_destroy_user

registration_controller:

def destroy
    @user = User.find(params[:id])
    if @user.destroy
      redirect_to root_path
    end
end

_backoffice.html.erb file:

<%= button_to 'Delete', admin_destroy_user_path(user.id), :class => 'btn btn-danger btn-sm', :method => :delete, data: {:confirm => 'Tem a certeza que quer apagar este utilizador?'} %>

And this is the error I'm getting:

NoMethodError in RegistrationsController#destroy

undefined method `name' for nil:NilClass

  def resource_name
    devise_mapping.name
  end
  alias :scope_name :resource_name

Thanks for any help.

Aucun commentaire:

Enregistrer un commentaire