0
votes

I'm using Ruby on Rails 4 and the gem Devise. I added a Username to Users, by following this guide: https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign-in-using-their-username-or-email-address . The Username is unique. Whenever a new user signs up and wants to use a username, that already exists, he gets an Active-Record-error. Everything works, as it should.

Now to my problem: I want, that if a user chooses an already existing username, he should be redirected to the registrations/new.html.erb page with a flash message, rather than seeing the ActiveRecord-Error-Page. How do I accomplish this?

Edit: Code to: Users::RegistrationsController#create

      class Users::RegistrationsController < Devise::RegistrationsController
# before_filter :configure_sign_up_params, only: [:create]
# before_filter :configure_account_update_params, only: [:update]

  # POST /resource
   def create
     @username = params[:username]
     @usernames = User.all
     @usernames.each do |f|
       if f.username == @username
         redirect_to root_path
       end
     end
     super
   end
end
1
Add the code for your implementation of Devise registrations_controller#createMarsAtomic
I added the code to: Devise::RegistrationsController#create , Is this the code You asked for?Metaphysiker

1 Answers

1
votes

I found a solution to my problem:

I added this to my controller:

  # POST /resource
   def create
     @username = sign_up_params[:username]
     if usernameexists(@username)
     redirect_to root_path
     else
      super
     end
   end

And it seems to work this way.