Katie H
Katie H

Reputation: 2293

Devise remove new registrations while keeping edit registration

I'm using devise_invitable to only allow users to sign up if they are invited.

To remove the user registrations while keeping the option to edit or delete an account:

In my user model, I removed :registerable

# app/models/user.rb
class User
...
    devise :database_authenticatable, :recoverable, :rememberable
...
end

In my routes file:

# config/routes.rb
  devise_for :users
  as :user do
    get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
    put 'users' => 'devise/registrations#update', :as => 'user_registration'
  end

When trying to edit an account I get this error:

No route matches [PUT] "/users/edit.user"

When trying to delete an account I get this error:

No route matches [DELETE] "/users/edit.user"

How do I remove the routes and links to sign up while keeping the ability to edit a registration?

Removing registrations from the model alone is not enough.

Upvotes: 0

Views: 145

Answers (1)

Rahul Singh
Rahul Singh

Reputation: 3427

in your routes add

resources :users, only: [:edit,:update]

this will give you 2 routes

 edit_user    GET      /users/:id/edit(.:format)       users#edit
   user         PUT      /users/:id(.:format)            users#update

now in UsersController add following and if u are using rails4 then u need to permit params also

def edit
      @user = current_user
end
def update
     @user = current_user
     if @user.update_attributes(user_params)
       redirect_to @user
     else
        render :edit
     end
end

def user_params
    params.require(:user).permit(:username,:name,:email)   # add all params which u need to save and that must present in user params passed from forms
end

to make a form add edit.html.erb in app/views/users/

<%= form_for(@user) do |f| %>
   <%= f.label :username %>
   <%= f.text_field :username %>
   <%= f.label :name %>
   <%= f.text_field :name %>
   # add all other fields

   <%= f.submit "Update" %>
<% end %>

Upvotes: 1

Related Questions