Create user dashboard and add more user attributes to Devise gem - ruby-on-rails

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.

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

Rails - How do you display data from the devise Users table in the View files?

I'm trying to display a simple welcome message to the user when they log in such as, "Welcome, Anthony!".
I created first_name and last_name attributes on the sign up and migrated them to the devise Users table and permitted the new attributes through the ApplicationController with this code,
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, :first_name, :last_name])
end
end
This works perfectly fine, however, I want to display the first_name in my navbar in the application.html.erb file, which acts as a welcome message to the user once theyve signed in.
How do I display info from the Devise Users table?
Thanks,
Ant.
(If I need to add anything else then just let me know!)
Once you logged in you should be able to access a current_user object, from which you can get first_name and display in the welcome message in the application layout.

Devise: making additional parameters required

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()

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