My First Rails App

This blog post will be discussing the process of completing my third flatiron coding project. It will go into detail on the project I created and the difficulties I encountered.

This project focused on using Rails (a Ruby framework) and developing a web application. Since my last project was more social media-focused, I wanted to create something game-related this time around. So I developed a Game review site.

Just like in Sinatra we were introduced to ULR's, GET and POST requests, etc. However, this time around, since Rails is fitted for a larger web application we got cool new features like its generator property that pre-populated your rails app with models, migrations, views, and helpers needed for the project, my favorite was "rails g resource ...".

This project consisted of building an app that had Rails associations, which is a connection between two Active Record models. Allowing users to signup, log in through the app, or through a third-party signup/login (OmniAuth), and the ability to log out. Some methods required were the use of Scope and helper methods within the app. I will be going into each of these requirements below.

  1. Rails associations: This part was what I wrote out first to make sure I had models that could create the associations needed for this project -Include at least one has_many relationship -Include at least one belongs_to relationship -Include at least two has_many through relationships
  1. Log In/Sign Up: This was one of the harder requirements
    because we needed to include a way to log in or sign up
    through a third party. I used Omniauth to include this
    feature. This article helped me set it up, and retrieve the

    user information need to log/sign them.

    Omniauth Article Link:
    https://medium.com/@jenn.leigh.hansen/google-oauth2-for-rails-ba1bcfd1b863

  2. Helper Methods: A helper is a method that has reusable code,

    which can be applied to many different places in your rails
    app (primarily Controllers and Views).Rails also come with
    a set of built-in helper methods. I used 2 helper methods,
    which I defined in my Rails-App/app/helpers/application_helper.rb file.

Then I placed a line of code in my Applications Controller, to make sure my helper method could be used on all controllers or views "include ApplicationHelper"

Here is an example of how the current_user is being used. The screenshot below is of my navigation. I used an if statement to call current_user to make sure there was a current user or user was logged in (I could have also used the logged_in? helper too). This is so that users can see a different nav depending on if you are logged in or not.

Scope: Adds a class method for retrieving and querying objects. I use Scope to alphabetize my games by their titles. Scopes are defined in the model that they are used for. Below is an examples of a scope in the games model.

This is how the scope is being called in the games controller

This is the Result: The games are now in alphabetic order

One of the most difficult things about learning Rails is that there are a lot of moving parts, so things can get messy quickly. With so many views, and models, and associations it can get confusing at times. So some tips that I learned along the way are 1. stay organized by tackling one model and it's views at a time. 2. byebug and binding.pry are lifesavers, so be sure to uses these gems if you are stuck.

26