Improve Nav Bar to Include Welcome Message for Users


I'm terrible with names.  Honestly most people are because it is easy to let your mind drift and not concentrate or focus on the name when meeting people.

Computers, thankfully, don't have this problem.  If you tell them your name, the remember it.

Currently our system doesn't know our users names though.  So to make our site more welcoming, lets start by adding a name field to our User model.  We do that by using a Rails migration which is the mechanism for updating the database schema when you want to make changes.

Using a generator, Rails can autogenerate the code to do this,.  From the command line within your project run the following command:

rails g migrations AddNameToUsers full_name:string

Using this naming convention, Rails can automatically determine you want to update the users table by adding a full_name column to it that is a string.

Next run...

rake db:migrate

... to actually update the database with this new field.

Next we need to update our view to ask a user signing up for our application for their name.  We do that in the app/views/devise/registrations/new.html.erb file.  Add the following code snipped to that file:

<div class="form-group">
  <%= f.label :full_name, 'Name' %><br/>
  <%= f.text_field :full_name, autofocus: true, autocomplete: "full_name", class: "form-control" %>

Now that our view can send in the full_name value, let's update our controller to be capable of accepting this value.  We can do that by adding two snippets of code to the app/controllers/application_controller.rb file.  Start by adding the following line near the top of the controller:

before_action :configure_permitted_parameters, if: :devise_controller?

Then add the corresponding configure_permitted_parameters method as a private method more toward the bottom of this class:

def configure_permitted_parameters
  devise_parameter_sanitizer.permit(:sign_up) do |u|
    u.permit(:email, :password, :password_confirmation, :full_name)

This tells the application controller to accept certain parameters, but only if the controller is a Devise controller; remember... every controller in our application extends from this ApplicationController so we include the if condition on this before_action so it is only run for a devise controller.

Note that the key here is that now we are allowing the full_name value to be permitted.

Now we can test our form!  If your server is not running, start it by running rails s.  Then make sure you are logged out and click our Sign Up link.  You now should be able to sign up as a new user and provide your name.

After signing up with a name we can provide a warm welcome to users that are signed in.  Update the app/views/shared/nav_menu.html.erb file with this snippet of Ruby/HTML:

<% if user_signed_in? %>
    <span class="navbar-text">Welcome <%= current_user.full_name %>!</span>
<% end %>

Note that we only want to display this welcome message if the user is signed in.

Now you should be able to see this warm welcome message in the nav bar!!

And guess what?  The computer won't forget the user's name and randomly call Will, Jim Bob!!

You're welcome! :-)

Happy coding!