Device Authentication - How to extend the sign up form? - ruby-on-rails

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

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.

Updating custom user attributes with existing Devise users/edit functionality

My users have a name alongside regular Devise columns like email, password, etc. I would like users to be able to edit their own account settings—including their name—at /users/edit, which Devise thankfully provides for us out of the box as part of registrations.
I added a name input to the view (simplified here):
= simple_form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f|
= f.input :name, required: false
= f.input :email, required: true
= f.input :password, autocomplete: 'off', required: false
= f.input :password_confirmation, required: false
= f.submit 'Save'
This renders the name correctly in the form, and I can edit and save, however the name is not updated while other things are. I imagined that Devise would simply take whatever params are in the form and update the resource with those params regardless of what they were, but that doesn't seem to be the case here.
I have got it working by overriding the controller (again only most important code shown):
class RegistrationsController < Devise::RegistrationsController
def update
resource.name = params[:user][:name]
resource.save
super
end
end
This works okay, but is there a simpler, built-in way to do this I'm missing?
Yes there is a simpler way. Make a new initializer, preferably config/initializers/devise.rb, and write the permitted parameters in that. A basic configuration looks like the following:
# frozen_string_literal: true
module DevisePermittedParameters
extend ActiveSupport::Concern
included do
before_action :configure_permitted_parameters
end
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
devise_parameter_sanitizer.permit(:account_update, keys: [:name])
end
end
DeviseController.send :include, DevisePermittedParameters
It allows module-wise configuration of permitting custom parameters. You can find more details about it here.
if you are Looking for simple and easy then try it,
In Application controller add following code snippet which will add your name attribute in strong parameter list
# app/controllers/application_controller.rb
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:account_update) << :name
end
hope it will help you.

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.

Adding Custom Attributes to Devise User Model

I'm using Devise with my Rails 4.2 application and I have custom users controller than I use to navigate to a profile page in the users#show route. The problem is, if I call
#user.username
It returns nothing and the value is nil.
Routes:
devise_for :users
resources :users, :only => [:show]
I also made the following adjustments on the config file via the application_controller:
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:email, :password,:username) }
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation,:username) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :password, :password_confirmation,:username) }
end
The weird thing is, I've seeded the db with default users and they have access to the method. So that leaves me to believe there's a problem with the form. But sure enough, I've enabled the field so it's not like I'm not passing in values.
devise/registrations/new.html.erb
<div class="field">
<%= f.label :username %><br />
<%= f.text_field :username, autofocus: true %>
</div>
Finally, I pass along the user object via the controller like this:
def show
#user = User.find(params[:id])
#posts = #user.posts.last(5)
end
One last thing, if I inspect the user object, it shows that there's a username method but the value is nil.
I've looked at the examples from Devise but still am not getting what's going on.
Very simple solution actually. The method needs to be called with a before action like this:
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :username
end
end
It's in the docs here: https://github.com/plataformatec/devise#strong-parameters
Did you maybe add attr_accessor in your user model?
This may override your values to be nil.

Devise not asking for extra fields on sign up

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.

Resources