Flatiron — Rails Project Week

Diana M.
4 min readJan 25, 2021

This week students were tasked with building their first Ruby on Rails project. I can honestly say that this was the most difficult task to date. Below I will describe my process for completing the project. To begin, the following were the requirements for the project:

Requirements:Use the Ruby on Rails framework.Your models must:• Include at least one has_many, at least one belongs_to, and at least two has_many :through relationships• Include a many-to-many relationship implemented with has_many :through associations. The join table must include a user-submittable attribute — that is to say, some attribute other than its foreign keys that can be submitted by the app's user• Your models must include reasonable validations for the simple attributes. You don't need to add every possible validation or duplicates, such as presence and a minimum length, but the models should defend against invalid data.• You must include at least one class level ActiveRecord scope method (Links to an external site.). a. Your scope method must be chainable, meaning that you must use ActiveRecord Query methods (Links to an external site.) 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.• You must include a nested new route with form that relates to the parent resource• You must include a nested index or show route• Your forms should correctly display validation errors.  a. Your fields should be enclosed within a fieldswitherrors class  b. Error messages describing the validation failures must be present within the view.• Your application must be, within reason, a DRY (Do-Not-Repeat-Yourself) rails app.• Logic present in your controllers should be encapsulated as methods in your models.done • Your views should use helper methods and partials when appropriate.• Follow patterns in the Rails Style Guide (Links to an external site.) and the Ruby Style Guide (Links to an external site.).-Do not use scaffolding to build your project. Your goal here is to learn. Scaffold is a way to get up and running quickly, but learning a lot is not one of the benefits of scaffolding.

The first day of the project I focused on outlining my models and their relationships. I decided to revamp my friend’s makeup website — currently her website only lists the services she does (bridal, special occasions, etc). I decided to create a website that will allow her clients to create an account and write reviews on her services. To meet the requirement of a “user submittable attribute” for the join table, I decided to allow others to leave comments on reviews. In this way my friend’s site would also service as a community, and hopefully lead to loyal clients who would filter in more traffic through positive reviews.

The second day I began the skeleton for my project. I created my new rails app in my terminal and wrote out the relationships for my models:

class User < ApplicationRecord
has_many :reviewshas_many :comments
has_many :commented_reviews, through: :comments, source: :review
___________________________________
class Review < ApplicationRecordbelongs_to :userhas_many :commentshas_many :users, through: :commentsend
___________________________________
class Comment < ApplicationRecordbelongs_to :userbelongs_to :reviewend

I then created my controllers and views for each model. At this point there was no logic in them, I was ensuring that I would have the necessary files and folders for each model. Once I was confident I had the skeleton for my project laid out, I set up my routes:

Rails.application.routes.draw doroot "sessions#home"get '/signup' => 'users#new'post '/signup' => 'users#create'get '/login' => 'sessions#new'post '/login' => 'sessions#create'delete '/logout' => 'sessions#destroy'resources :comments
resources :users do #NESTED ROUTESresources :reviews, only: [ :new,:create,:index ]end
resources :reviews doresources :comments, only: [:new,:create,:index]endend

When creating your routes, make sure to list any root or custom routes first to avoid any errors. Much like Sinatra, the order of URLs matter.

After this, I began working on my User’s controller and views first. I set up the functionality to sign up for an account, log in if you have an existing account, and sign out. I followed the same flow to allow a user to create a review for a service, see all their reviews, as well as edit or delete a review. Once I ensured my application was successfully doing these tasks, I began to work on a user being able to leave a comment on a post, as well as view all the comments they’ve made on previous posts. This proved to be quite tricky for me as their was nested routes involved. Google and Youtube were the best resources in helping me get through this hump!

At this point I was in the middle of the week, and I worked on the logic for all of my views. I also worked on the navigation bar for my users. I wanted a user to see a navigation bar when they were logged in and another when they were logged out. I did this by creating a helper method called ‘if_logged_in?’ in the User.rb model.

Towards the end of the week I focused on making sure my application had the proper validations and authentications.

In the end my application was able to successfully run and do the tasks I wanted it to do! However, it has tremendous potential in my opinion to have a strong design and a more user friendly layout. This is something I’ll continue to work on over the few days.

This project was quite difficult, but the only way to learn is to just do it. I utilized Google, Youtube, and my peers to help get through any humps. Though I did not struggle with errors too much, I would say that my biggest challenge came with figuring out the logic of certain actions when nesting was involved. Overall it was greatly exciting to build an official application!

--

--

Diana M.

Software Engineering graduate with a penchant for matcha lattes and baked goods. Most of my content will consist of RoR, JS, HTML/CSS, and React.