Locked lesson.
About this lesson
Adding user names to our Devise database table.
Exercise files
Download this lesson’s related exercise files.
Adding a Name To Devise.docx58.7 KB Adding a Name to Devise - Solution.docx
59.4 KB
Quick reference
Adding a Name to Devise
In this videos we'll add a name field to our devise forms and connect it to the database.
When to use
When we created our devise user migration, we added a name field. Now we need to make that name field functional by adding it to our forms and connecting it to the database through the controller.
Instructions
For the User Sign Up Form, and the Edit Profile form, add this code:
<div class="form-group">
<%= f.label :name %><br />
<%= f.text_field :name, autofocus: true, class: 'form-control' %>
</div>
Next: To connect that form field to the database, we need to modify our application controller at app/controllers/application_controller.rb:
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
devise_parameter_sanitizer.permit(:account_update, keys: [:name])
end
Hints & tips
- To add a user name, we need to add a name input field to our forms
- We also need to make a change to our application_controller.rb file
- You can copy and paste the application_controller.rb code directly from the devise documentation
- Besure to modify the devise documentation code accordingly
Lesson notes are only available for subscribers.