Devise not asking for extra fields on sign up - ruby-on-rails

I'm new to Devise, was advised against using my own authentication and use this instead since it's the standard for Rails apps and I'm having a hard time with it.
I'm trying to get users to signup to my site but I need to ask for more fields than the Devise defaults. I added this to my applications controller:
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :username, :email, :password, :password_confirmation, :remember_me) }
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :username, :email, :password, :remember_me) }
end
Yet still only the email, password and confirmation show up. Am I missing something?

Run rails generate devise:views to add devise views in your application code for customization.
After running this command a folder would be created in your_application/app/views directory named devise. It will contain all the devise views which you can then customize as per your requirement.
For example: To add new fields on sign up page, you would need to customize your_application/app/views/devise/registrations/new.html.erb view.

Related

Confusion regarding custom fields in devise user in ruby on rails?

I was studying how to add custom fields in our devise user model in ruby on rails and I came across this link I just have one doubt regarding the following piece of code-
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:email) }
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :title_id, :province_id, :first_name, :last_name) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :password, :password_confirmation, :current_password) }
end
This was written in application controller, In case of :sign_in we only check for email and why not password and in :account_update why don't we check for first_name and last_name after all a user can update his first_name and last_name as well.
Can somebody please help me, How these 3 lines work?
Sign-in: The sanitizer knows about Devise default parameters like password or password_confirmation. Therefore you don't need to add password in the list.
Sign-up/account-update: You can add first_name andlast_name or change the permitted parameters list on your controllers.
Please See this documentation
Note: Only given parameters will be allowed to process request further.

Devise authentication with email as usual, but forcing the user to put a name

Well, to contextualize a bit, I want devise acts just like always, the only difference being that when a user sign up, he have to put a name (and it doesn't have to be blank)
What I have achieved is that when the user sign up he can put a name, the problem is that he can leave it blank and his name will be equal to "".
I did this with some help of what appears on this page, ie creating a migration to add the name column to the users table and putting the following in the application controller.
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :email, :password, :password_confirmation, :remember_me) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:name, :email, :password, :password_confirmation, :current_password) }
end
end
But what is explained there later is not exactly what I want to do, I want the user to always sign in with his email (as devise already does by default), as I said, the only thing I wish is that if anyone wants to sign up, he have to type some name, being this not blank.
Does anyone know how can I do it?
what you need to do is to add validation for name to the user model. Validate the presence of name on a user
So, in your user model:
validates_presence_of :name
with this, when a user submits a registration form with blank name, this validation will kick in, and prevent this user instance from being saved, while adding the "name can't be blank" error to the form.

Device Authentication - How to extend the sign up form?

I would like to extend my sign up form with elements like:
Username
Firtname
Lastname
Age
Currently available is only:
Email
Pass
ConfirmPass
How can I do that?
You need to do 2 things.
Just add those new fields to the generated Devise form
app/views/devise/registrations/new.html.erb
You'll need to add those new fields to the Devise strong params. You can do that by adding this to your ApplicationController
def configure_permitted_params
devise_parameter_sanitizer.for(:sign_up) { |user| user.permit(:username, :first_name, :last_name, :age :email, :password, :password_confirmation) }
end

Unable to login via Devise after `devise_parameter_sanitizer` set in rails4

Previously, I've discussed an issue here that enables login via username my app with rails 4. In 'Strong Parameters' part of README of Devise, "the lazy way™" says additional parameters like "username" could be added with configure_permitted_parameters under ApplicationController. Then I copy-pasted code below the example into my app respectively, but it worked only :sign_up phase. So, I add other phases like this:
def configure_permitted_parameters
devise_parameter_sanitizer.for(:account_update) << :username
devise_parameter_sanitizer.for(:sign_in) << :username
devise_parameter_sanitizer.for(:sign_up) << :username
end
But it didn't worked for any phase, so I removed all lines, even deleted the function, but nothing turns it back. How can I fix it?
Devise version?
Try:
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:email, :password, :remember_me) }
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password) }
end

Adding Username to devise rails 4

I have a rails 4 application using devise. I'm trying to allow users to have a username associated with them.
I've added a username (string) column to the Users table, and had my Application controller look like this:
class ApplicationController < ActionController::Base
before_filter :configure_permitted_parameters, if: :devise_controller?
protect_from_forgery with: :exception
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:username, :email) }
end
end
and I've also added a field for the username on the users/sign_up page.
But I get this error:
undefined local variable or method `devise_parameter_sanitizer' for #<Devise::RegistrationsController:0x00000101378a28>
So basically my question is why is this error appearing, or how else can I get a user to get a username?
Thanks for all help!
You're only permitting the username to be allowed on the sign in method, but I'm assuming when you create a new user, it's on the sign up method. So try this:
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation) }
end
Source: https://github.com/plataformatec/devise#strong-parameters

Resources