FlatironStudents: Ruby on Rails Project
In the third phase of my journey in Flatiron’s Software Engineering Bootcamp, we were tasked to build a complete Ruby on Rails Content Management System (CMS) application. This was by far one of the toughest projects I have tackled in my journey to becoming a Software Engineer, and I am so grateful to have learned so much through it.
Project Requirements
- Use the Ruby on Rails framework
- Your models must include at least one
has_many
, at least onebelongs_to
, and at least twohas_many :through
relationships. - Your models must include reasonable validations for the simple attributes. You don’t need every possible validation or duplicates, such as presence and a minimum length, but the model should defend against invalid data.
- You must include at least one class level ActiveRecord scope method. Your scope method must be chainable, meaning that you must use ActiveRecord Query methods within it (such as
.where
and.order
) rather than native ruby methods (such as#find_all
or#sort
). - Your application must provide standard user authentication, including signup, login, logout, and passwords.
- Your authentication system must also allow login from some other service. Facebook, Twitter, Foursquare, GitHub, etc…
- You must include and make use of a nested resource with the appropriate RESTful URLs which should include routes to a nested
new
,index
, andshow
. - Your forms should correctly display validation errors.
- Your application must be, within reason, a DRY (Do-Not-Repeat-Yourself) rails app.
- Do not use scaffolding to build your project.
Project Overview
FlatironStudents is a Ruby on Rails CMS web application built with the objective of connecting Flatiron students across multiple cohorts and multiple programs. The application allows students to share the projects they have built throughout the five phases of Flatiron Schools Bootcamp, submit and receive surveys regarding the projects they have built which will be visualized for each student in their projects page, the survey submissions will of course be anonymized. It also features an activity dashboard that tracks Create, Update and Delete actions within the users and projects databases. (I will be expanding this to track the cohorts table as well as the forum section soon). The application also features a forum section were students will be able to create and have discussions, and last but not least, an admin user and dashboard (one user to rule them all).
Models, Associations, and Database Schema:
We will be using ten models for this project and the associations between each of them are as follows:
User
belongs_to :cohort
belongs_to :role
has_many :projects
has_many :survey_responses, through: :projects
has_many :posts
has_many :comments, through: :posts
Cohort
has_many :users
Role
has_many :users
Project
belongs_to :user
has_many :survey_responses
Survey
has_many :survey_questions
has_many :survey_responses
SurveyQuestions
belongs_to :survey
SurveyResponse
belongs_to :survey
belongs_to :project
Category
has_many :posts
Post
belongs_to :category
belongs_to :user
has_many :comments
Comment
belongs_to :post
belongs_to :user
Database Schema
User Authentication
For this part of the project, we will be utilizing Devise, a flexible authentication solution for Rails based applications.
- Add the following into your Gemfile and run bundle install.
> Gemfile
gem 'devise'> Console
$ bundle install
2. Run the devise generator below to generate our devise local and initializer files.
$ rails g devise:install
3. Let’s run the devise generator again and pass in a model name of User
. This should generate our devise model and migration files for our User
model, as well as update our routes.rb
file to include :users
.
$ rails g devise User
4. Next, let’s generate our devise controllers to the scope of users with the following command.
$ rails g devise:controllers users
5. Once everything runs successfully, let’s run the command below to migrate our database into our rails application.
$ rails db:migrate
Omniauth with GitHub
In order to add Omniauth with GitHub, we must first sign-in to GitHub and navigate to the developers console and register our new application.
- Click on “New OAuth App” to register our application.
- Fill-up the form your applications information. For now let’s use our rails server as our URLs.
- Homepage URL: http://localhost:3000/users/auth/github/callback
- Authorization callback URL: http://localhost:3000/users/auth/github/callback
3. Click on “Generate a new client secret” and copy the values of both the client id and client secret.
4. Add the following gems into our Gemfile and run bundle install.
> Gemfile
gem 'omniauth'
gem 'omniauth-github'
gem 'omniauth-rails_csrf_protection'
gem 'dotenv-rails'> Console
$ bundle install
5. Create a .env file within the root directory of your application and insert the values of your github’s omniauth client id and secret as follows:
6. Update our devise.rb file located in our initializers folder with the following line.
7. Update our routes.rb and insert the following lines into your devise_for :users
.
devise_for :users,:controllers => {omniauth_callbacks: 'users/omniauth_callbacks'}
8. Update our devise generated controller omniauth_callbacks_controller.rb
and insert the following lines of code.
omniauth_callbacks_controller.rb
9. Update our User model to include the following lines of code.