Complete the code to add Devise to a Rails model for user authentication.
class User < ApplicationRecord [1] end
has_secure_password instead of Devise's devise method.The devise method inside the model sets up authentication modules like database authentication and registration.
Complete the command to install Devise in a Rails app.
bundle add [1]pundit or omniauth instead of Devise.bundle install after adding the gem.The command bundle add devise adds the Devise gem to your Rails project.
Fix the error in the Devise route setup by completing the blank.
Rails.application.routes.draw do
[1] :users
endresources :users which does not set up Devise routes.get or root_to.Devise requires devise_for :users in routes to set up authentication paths.
Fill both blanks to add authentication checks in a controller.
class PostsController < ApplicationController before_action [1] :authenticate_user! def index @posts = Post.all end end
skip_before_action which disables filters instead of adding them.except with only.The before_action :authenticate_user! runs authentication before actions.
Fill all three blanks to customize Devise's permitted parameters in ApplicationController.
class ApplicationController < ActionController::Base before_action [1] :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.[2](:sign_up) do |user_params| user_params.permit(:email, :password, :password_confirmation, [3]) end end end
permit! which allows all parameters and is unsafe.if: :devise_controller? to limit the filter.:username.The before_action calls the method to configure parameters. The permit method allows extra fields like :username during sign up.