Devise: making additional parameters required - ruby-on-rails

According to latest Devise docu, I added additional parameter :name to my User model and adjusted RegistrationsController. It works so far, the parameter is taken, and I am able now to register the user with a name, but how I make this parameter strong required? If I do not specify it in the registration request, it still register the user without setting the name (in db then: name = nil). So how to prevent this situation and make name required?
here is the controller:
class Users::RegistrationsController < Devise::RegistrationsController
# Disable CSRF protection
skip_before_action :verify_authenticity_token
# Be sure to enable JSON.
respond_to :html, :json
before_action :configure_sign_up_params, only: [:create]
protected
def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
end
end

For this you have to apply validations on the sign up form. You can use two different ways for this
Server Side Validation
For this you can use validates helper. You can add following line in your user model (user.rb). Refer this link for more information.
validates :name, presence: true
Client Side Validation
You can use JQuery plugin like for achieving this functionality.
Please refer this link for implementing JQuery validation using jquery.validate()

Related

How to add custom devise fields/ Access devise default controller

I'm trying to use Devise gem in my project. That worked, but I still have a problem:
I setup devise in my application and generated the views, but I added an extra field in the database (username). So, the thing is that I need this username, but it's not being saved in the database when I create a new user :/ The problem seems to be in the controller, but I don't know how to access it.
Do devise hide its controllers? Can I access this controller and simply add the field I want in the params, for example? Do I really need to code a new controller that will override the default one? What should I do? and how?
See doc: https://github.com/plataformatec/devise
You have to tell the devise to save extra parameters in the DB.
Your application_controller.rb file look like:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
end
end

User birth date params not saving

I have a small rails app, but i'm having trouble getting some information that i'm trying to implement into the user to save. My app as of right now asks for a username and password for signing up. I did this by using the devise gem. However I want to also add an age check to the app, and this is where i've been running into problems.
My DB column is set at (in my schema)
t.date "birth_date"
My view is at
<%= f.input :birth_date, required: true, start_year:1900 %>
My controller has two parts to it:
def create
#user = current_user.build(user_params)
end
private
def user_params
params.require(:user).permit(:birth_date)
end
Whenever i try to log in and create a birth_date for my new user, in the console when I check, the birth_date is still set at nil. Would anyone know what i am missing with this? (I think i'm getting tripped up with Devise handling some things already)
Assuming you are trying to do input birth_date of the user as a part of sign_up process and using devise for this purpose you need to whitelist birth_date params explicitly.
Best way of doing that is using your own registerations_controller and inheriting it from devise
class Users::RegistrationsController < Devise::RegistrationsController
before_action :configure_sign_up_params, only: :create
before_action :configure_account_update_params, only: :update
protected
# If you have extra params to permit, append them to the sanitizer.
def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up, keys: [:birth_date])
end
# If you have extra params to permit, append them to the sanitizer.
def configure_account_update_params
devise_parameter_sanitizer.permit(:account_update, keys: [:bith_date])
end
end
#in routes.rb
devise_for :users, controllers: {registrations: "users/registrations"}
since you using devise, and additional field while register, I think you should overwrite devise, here is link for reference, create registration controller as follow
app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def create
super do
resource.birth_date = params[:birth_date]
resource.save
end
end
end
make sure you also have devise routes
app/config/routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}
Assuming this your UsersController, separate from Devise controllers, you just need to save it to db:
def create
#user = current_user.update(user_params)
end
But you might need to change the birthday in params (which is a string) to a date object first, depending on your column type.
Also you might consider moving this to #update as this is really more an #update action than #create.

Create user dashboard and add more user attributes to Devise gem

I have used the 'Devise' gem provided by Rails to for user SignUp/Log In. By default it provides 'email', 'password' and 'password confirmation' fields. I wish to add more fields like 'Nickname', 'City', 'Company' etc and then also create a user profile page (Dashboard) which simply displays this information.
Can you please tell me in detail(at least mention the steps) as to how I can achieve this?
As Iceman stated, you can find how to add parameters at https://github.com/plataformatec/devise. You will have to generate the views and edit the forms to include your parameters. Then the "lazy way" is too add the following code in your Application controller.
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
end
end
To create a profile page, you can just add an action to one of your existing controllers or one of the devise controllers.

Using Strong Parameters With Devise

I have used Devise for authentication.
Went ahead and updated my Users database to include a bio column. Added this and ran the migration so I can see it is there.
Now I want Users to be able to add a bio once they are logged in. Did some research and I see attr_accessible is no more in rails 4 and I should use strong parameters. Also checked out the Devise documentation but couldn't quite find what I am after.
I see they are added to the controller but as I have used Devise I don't have access to the UsersController
How can I add the ability for Users to update the bio field so it saves in the database?
You should add a before_filter in your ApplicationController to do that. Devise docs contains a section explaining this. I took the code below from there:
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
In the example, the attribute :username is allowed to be parsed in the sign_up page.

Rails - add a check box to form and check if it's checked on post

I have a common problem: I am using devise and I want to add a checkbox for "terms and conditions".
It's well covered everywhere, except for one moment. If I do
<%= f.check_box(:terms_and_conditions )%>
Then I get an error that :terms_and_conditions is not defined. Where should I define it? The model? The controller?
Try using the acceptance validation method:
class User < ActiveRecord::Base
validates :terms_and_conditions, acceptance: true
end
As stated in the Rails Guide
This validation is very specific to web applications and this 'acceptance' does not need to be recorded anywhere in your database (if you don't have a field for it, the helper will just create a virtual attribute).
In addition, you'll need to add the :terms_and_conditions parameter to the permitted parameters for sign up:
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :terms_and_conditions
end
end

Resources