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
registrations_controller#create
– MarsAtomic