0

Situation:

I have an Account model which is a devise model and I'm looking to disable users from being able to register themselves. There is a similar question on SO already but the proposed answer is just not working for me and it also does not have any templates in the answer.

Here's my setup:

registrations/edit.html.erb

<%= form_for resource, as: resource_name, url: account_registration_path(resource_name), html: { method: :patch } do |f| %>
...
<% end %>

I had to change the url: from registration_path to account_registration_path for the form to even render.

The problem is I cannot submit the form without it throwing an error. It errors out with:

ActionController::UnknownFormat in Devise::RegistrationsController#update

routes.rb

  devise_for :accounts, skip: [:registrations]
    as :account do
      get    "accounts/edit"     => 'devise/registrations#edit',    as: 'edit_account_registration'
      patch  "accounts"  => 'devise/registrations#update',  as: 'account_registration'
      #delete "account"  => 'devise/registrations#destroy', as: 'destroy_account_registration'
    end

Is it possible to somehow set this up in such a way that the url paths for editing the profile are identical to the ones devise supplies when registrations is not disabled?

I still want users to be able to edit/cancel their account, just not register.

AntelopeSalad
  • 1,736
  • 1
  • 16
  • 27

1 Answers1

0

In your devise model (Account), you can remove (or comment out) the registerable plugin from the devise modules section. An example of what it could look like before would be this:

devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

And after:

devise :database_authenticatable, #:registerable,
     :recoverable, :rememberable, :trackable, :validatable

You can read more about that module here.

If you just want the register ability removed, here is another workaround. You can create your own DeviseRegistrationsController, like is demonstrated here, and then in the new action, just set a redirect to the root URL. You can also delete the view (if you have generated them).

Community
  • 1
  • 1
Amar H-V
  • 1,316
  • 3
  • 20
  • 33
  • Yeah I know but that removes the ability for users to edit and cancel their account (imagine the case where an admin created their account but they are free to edit their own details). My above snippet manually remaps those routes back in but I just can't get it to work. I'm sure I can brute force it to work with different paths in the views but ideally I want the non-registerable version to use identical view templates as the registerable version. – AntelopeSalad Feb 05 '14 at 12:36
  • Yeah, I was hoping I would not have to resort to custom controllers. What I ended up doing was just redirecting somewhere else when someone tries to hit the end points that control creating a new account before I load up `devise_for` in the routes file. By the way deleting the generated views likely won't make a difference on its own. Due to how template inheritance works it will just pickup the view from the devise gem's view folder. – AntelopeSalad Feb 05 '14 at 18:58