Here you will set up a user login.
Go to your Portfolio project docker environment and cd to your portfolio project directory.
- Add the devise gem to your Gemfile after gem "rails"
gem "rails" , "~> 6.1.7"
gem "devise"
- Refresh/ install all the gems in your project
bundle install
- Set up devise in your project by running the generator command to create all the necessary logic and views
associated with devise
bundle exec rails g devise:install
- Open config/initializers/devise.rb and add the following line of code. Make sure that the code is within the devise
setup loop. Your code should look something like the following. The line of code should be added somewhere around line 19.
Devise.setup do |config|
# ... comments...
config.navigational_formats = ['*/*', :html, :turbo_stream]
# ... comments...
end
- Run the following generator command to generate the devise user model
bundle exec rails g devise user
- Confirm that the following code exists within your config/routes.rb file above the code for
root "home#index
if it does not exist, make sure to add it.
devise_for :users
root "projects#index"
resources :projects
-
To view all the available routes, we can run the following command and get an output in the command line of all possible routes that we can provide to the user
bundle exec rails routes
-
Make all the database migrations and place the user's table in the database
bundle exec rake db:migrate
-
Generate the views associated with devise by running the following command.
rails generate devise:views users
-
In /config/initializer/devise.rb, Uncomment the following around line 255 # config.scoped_views = false change false to true
config.scoped_views = true
-
Start rails and go to http://localhost:3000/users/sign_in to see the login page
-
Open app/controllers/projects_controller.rb and update the following
class ProjectsController < ApplicationController
before_action :set_project, only: %i[ show edit update destroy ]
before_action :authenticate_user!, only: %i[ new edit create update destroy]
- Open app/views/layouts/application.html.erb and add the following code inside the HTML body tags.
# Your code should look something like this
...
<body>
<p class="navbar-text float-right">
<% if user_signed_in? %>
Logged in as <strong><%= current_user.email %></strong>.
<%= link_to 'Edit profile', edit_user_registration_path, :class => 'navbar-link' %> |
<%= link_to "Logout", destroy_user_session_path, method: :delete, :class => 'navbar-link' %>
<% else %>
<%= link_to "Sign up", new_user_registration_path, :class => 'navbar-link' %> |
<%= link_to "Login", new_user_session_path, :class => 'navbar-link' %>
<% end %>
</p>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%= yield %>
</body>
-
View the changes that you made by restarting your server and navigating to http://localhost:3000/ and test that you can't create new, edit update or destroy
-
Open the app/views/projects/index.html.erb file and update to only show edit destroy new buttons buttons if logged in. Here is the code to determine if logged in
<% if user_signed_in? %>
<% end %>
- Test the changes that you made for not displaying the buttons
- In your application's directory, create a new directory with name
.circleci
(including the period at the beginning of the name)
mkdir .circleci
- Create a new file inside the directory titled config.yml
touch .circleci/config.yml
- Copy and paste the following contentment from below into the config.yml file
# Use the latest 2.1 version of CircleCI pipeline process engine.
# See: https://circleci.com/docs/2.0/configuration-reference
version: 2.1
jobs:
build:
docker:
- image: 'tghastings/code-esaas'
steps:
- checkout
- run:
name: rspec
command: sed -i 's/ruby "[0-9].[0-9].[0-9]"/ruby "3.0.2"/' Gemfile && bundle install && bundle exec rake db:migrate RAILS_ENV=test && bundle exec rspec
# Invoke jobs via workflows
# See: https://circleci.com/docs/2.0/configuration-reference/#workflows
workflows:
sample: # This is the name of the workflow, feel free to change it to better match your workflow.
# Inside the workflow, you define the jobs you want to run.
jobs:
- build
- You will need to push your code to Github
- Go to https://circleci.com and Sign-in with GitHub
- Accept the Authorization of Circle CI (you'll be re-directed to GitHub and then back to CircleCI)
- Select the repository for your portfolio project (this should be listed in CircleCI's interface)
- Push your code with unit tests to GitHub
- Verify that you can see your tests passing on https://app.circleci.com/pipelines/github/<GITHUB_USERNAME>/<PROJECT_NAME>
- Make changes to the code in your project.rb file so that the tests will fail Do this by commenting out
validate_presence_of
class Project < ApplicationRecord
# require title and description
#validates_presence_of :title, :description
end
- Push your modified code back to GitHub and navigate back to your project in CircleCI https://app.circleci.com/pipelines/github/<GITHUB_USERNAME>/<PROJECT_NAME>
- See that your tests failed
- Click on the small triangle left of the red FAILED icon.
- Click on the blue build link.
- Expand the rspec box to view the test output.
- Revert the changes you just made in the project.rb file and uncomment the
validates_presence_of
line
class Project < ApplicationRecord
# require title and description
validates_presence_of :title, :description
end
- Push your code back to GitHub and re-check CircleCI to ensure that all the tests pass again