rails_logo

What is Ruby on Rails?


Ruby on Rails, or Rails, is a server-side web application framework written in Ruby under the MIT License. Rails is a model–view–controller framework, providing default structures for a database, a web service, and web pages.


History of Ruby on Rails


Ruby on Rails was originally extraceted from work done on Basecamp, a web-based project management tool, by David Heinemeier Hansson. He released the framework as open-source in 2004, but did not initially share commit rights until 2005. The framework gained notoriety when Apple decide to ship it with Mac OS X v10.5 in 2007. Since its release it has seen a large amount of upgrades and improvements the frameworks functionality. Introducing gems, templates, engines, Rack and nested model forms in 2009. This added functionality made Rails an intuitive and user friendly framework, that can create beautiful functional webpages quickly.


Why did we choose Rails?

Most sites that users are interacting with implement a simple CRUD(Create, Read, Update, and Destroy) structure. Sites like Twitter, Facebook, Instagram and Reddit, follow this structure. Users create a post, read other posts, update tehir status, and delete posts. Rails' functionality is invaluable in creating these types of sites(as you'll see in our tutorial). With simple command line executions we are able to create whole CRUD structures. Rails automatically generates the files, routes, and functions necessary to make these types of sites quickly and easily.


For our site, we wanted to create a CRUD application that allows users to seek out new music releases, get more information, update the reviews with their opinions or add new songs to the site, and to delete their previous reviews. With Rails we are able to create a beautiful and effective app in this style, easily.



How to setup Rails


Unfortunately, Rails was originally developed to be used with Mac's. So Windows installation is a little more difficult than on Mac.


What you will need before installing Ruby on Rails



*Install of these with the default settings*

Now we can use the Rails installer

Rails installer is great for getting us necessary software we need to use Ruby on Rails(Bundler Sqlite, TinyTDS, etc.). However, the versions of Ruby and Rails that come with this installer are out of date, so we will have to update them later.

Download and install the package containing Ruby version 2.3. Following all of the default prompts.


Now we can update Ruby


Go to rubyinstaller.org to download the latest version of Ruby.

The most recent, stable version is highlighted in bold. Follow all of teh default prompts, making sure everything is checked.

This will update the old version of Ruby we installed with Rails installer.


No let's update Rails


Open the Git Bash terminal and run the "gem install rails" command.


rails_install

When installed it will look like this.



Now lets make a test project and run the server to see if our install worked


First we need to make a directory for our projects. We are going to place it in our C drive.

$ mkdir /c/musicReviewSite

Next let's navigate into our new folder.

$ cd /c/musicReviewSite/

Now that we are in the correct directory, lets make our project.

$ rails new musicReviewSite

This command will create the necessary skeleton of our project.

new_site

You should see this message when complete.


Now let's navigate into our new project

$ cd musicReviewSite/

And now we can initialize the server to see if it works.

$ rails s

server

This output means the server is working.


In your browser enter:

localhost:3000


welcome

If you see this landing page, Congratulations! You made your first Rails site.



Now we can start building our site


We need to have a few things open to work on our project


When opening your project in Sublime you'll see Rails has created a number of folders and files for us


- We will mostly be using the App, Config, and DB folders. Along with the Gemfile


Now we need to create a Controller and a View for our homepage

Remember from earlier, Rails is an MVC framework. The Model being our database, Views are our actual pages we create, and Controllers execute the requests from our application. So to start we need at minimum a Controller and a view.

So we want to create a Controller and a View for our homepage. To do this Rails has generate helper files that we can use to easily generate the things we need.

So navigate to the Git Bash terminal for our musicReviewSite directory and execute the following command.


$ rails generate controller home index


generate_home

Rails has now executed all of these commands, creating the necessary files we need.


Now lets navigate to our newly created homepage.


new_homepage

We should see a page that looks like this.

Lets make this our root page. To do this we have to change the route. In Rails routes allow us to navigate to a specific page based on http requests.

To do this lets go to our text editor and open config/routes.rb.

homeroute

The routes file should look like this.


To make our new page our root, we need to edit this file. First comment our the line "get 'home/index'". Then add:

root 'home#index'

When comeplete 'localhost:3000' should now bring us right to our homepage.

Now we can edit our homepage and add whatever we like.



Developing our homepage and adding a nav bar

In order to start developing a styling our webpage we need to open the 'application.html.erb' file. This file is located inside app/views/layouts folder. This file is essentially our base template for all of our webpages. We can use this file to keep consistency with our site, while also making it dynamic.

application

Your application,html.erb file should look something like this.


In this file you'll notice a 'yield' tag, surrounded by '<%= %>'. This tag is used for embedding ruby. This tag is here for us to output the content we create for our other pages within the blueprint that Rails has created for us

Now within our views folder we also have a home folder. This will house our pages we create. If we navigate to index.html.erb we will see the text we saw when we created out homepage

homenew

Your index.html.erb file should look like this.

Within this file we will be adding all of the content we want on our homepage.

Now that we're more familiar, lets add a navbar

For the sake of the tutorial we will be using some Bootstrap elements to get us started.

For our navbar we are going to create what's known as a partial. This allows us to create a separate file containing only a navbar, foot, etc. And use it throughout our project. It's good practice in Rails to implement partials to enable code reusability.

To create a partial, right click on your app/views/home folder and create a new file. Then save this file as:

_header.html.erb

The underscore in front of the filename shows Rails that this is a partial.

Now lets a bootstrap navbar.

Bootstrap navbar

View the page source and copy and paste the code in the nav tags into your _header.html.erb file.

Now to get our navbar to display on all of our webpages, navigate back to application.html.erb page. And we are going to place an embbeded Ruby tag right above the yield tag from earlier.

header_render

Adding this tage will render our navbar on all pages

We can use this same technique to create a footer as well

Now lets edit our navbar to show the things we need.

links

Next we need to edit some of the included links. Change the home link to Albums, delete the other two. And finally edit the dropdown to show our album categories. We will be editing these again later when we add album models and views to our site.

Lets add some content to our homepage.

Our site's purpose is to be a community for music lovers to share their opinions on albums they love. So we want to add some content to our homepage to refelect that.

All of the following content will be added to our app/views/home/index.html.erb file. As this is the file designated for homepage content.

I started by adding a Carousel from bootstrap and added some content and images to kind of explain the purpose of our site to users.

We used to the carousel with captions for our site.

carousel

Here is the carousel code with all of the added text and images.

Next we added a card group with footers, to show some reviews on the homepage of new albums, to give a greater idea of site function.

card_group

Here is the card group code with all of the added text and images.



home_page

Now we should have a site that looks similar to this.


Lets add login functionality to our site.

In order to add sign up, log in, log out and edit progfile functionality, we will be using a gem called Devise. RubyGems is a package manager for Ruby that allows us to add in Gems, which are essentially libraries that you can install and use in your project. It's a huge aspect of the Rails framework and helps us add useful tools to our site without having to reinvent the wheel.

So first we want to take a look at the Devise documentation. Which can be found here. And navigate to the "Starting with Rails?" section. Here we will find valuable information to help us install the Devise gem.

In order to install the Devise gem, we need to edit our GemFile. First make sure you do not have the rails server running in Git bash terminal. Then in Sublime, navigate to the Gemfile.

home_page

We need to add the "gem 'devise'" line as it appears in this image.

Save your project and return to the Git Bash terminal for your root project directory. And run the following command:

$ bundle install


success

If all installed correctly you should see this screen.

Next run this command:

$ rails generate devise:install

This command will run but it will also give us some instructions that are essential to correct installation.

The first direction is to make sure that you have a default url option in your environments files. To do this we navigate to our config/environments/development.rb file. And at the end of the file, right before the last 'end' tag add this line:

config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

If you are publishing this site, you will add the same line in the config/environments/production.rb file, but with your host and url.

Next we need to configure a root_url in routes file. We already did that when we setup our homepage.

Next we need to configure flash messages. This will add functionality to show alerts on the page for when the user creates an account, logins in, etc.

To do this we need to create another partial. So in our app/views/layouts folder create a new file and save it as "_alerts.html.erb".

Next we are going to grab and alert from bootstrap and paste it into this file.

alert

We used dismissable alert in the bootstrap documentation.

Finally we need to add a render tag in our application.html.erb file

render_alert

Should look like this.

Lastly we need to setup Devise views and models.

To do this run this command in the Git Bash terminal:

$ rails generate devise:views

devise_views

This command should create all of these helper files in our app/views/devise folder.

The way devise works is it stores user login and session info into a database. We need to create a model for this functionality and push it to our dev database. To do this run the following command:

$ rails generate devise user

devise_user

Rails has now generated all of these files for our db model.

Now we need to migrate our database model to the database. Run this command in the terminal:

$ rails db:migrate

Lastly we need to setup routes so we can now acces the login functionality and add them to our navbar. And while we do this we will use the "user_signed_in?" devise function to make our navbar dynamic. To do this navigate back to the header partial file we created earlier and with in your navbar unordered list tags that contain all the navbar links.

login_routes

Add this code into your header file.

What this code does is setup navbar links that route to the devise login, logout, edit profile and signups views. Also since we created if statements using the "user_signed_in?" function, when a usere is not logged in, they only see sign up and login. And when they are logged in they see, sign out and edit profile.

If we start our rails server and navigate back to our page.

devise_home

Our homepage should now have the Sign Up and Sign in Buttons.


sign_up

Sign Up page.


sign_up_alert

Successful signup with alert working.

Lets add in our Album model.

We need generate an album model in our Git terminal:

$ rails g model album title:string artist:string songs:string

album_model

Rails will generate all of these helper files.

Then we need to migrate our model to the Database:

$ rails db:migrate

Next a route to our routes.rb file

albums_route

Now our URL can access the albums model

Now in our app/views/albums folder we need to create a new file and name it index.html.erb. This will be the page we go to when we navigate to albums.

We now need to add the simple-form and bootstrap-sass gems to help us style the CRUD scaffold of our applciation.

forms_gem

Add these to your gem file.

Then run bundle install in the Git terminal

Then we need to creat a controller to handle the CRUD structure.

albums_controller

This is a simple controller, we will be adding more to it.


forms_edit

In the form partial view, we then need to add the code for the inputs of a new album.

Lets assosciate Users with albums.

To do this we need to generate a migration that adds user_id to albums. To do this we run this command in the Git bash Temrinal:

$ rails g migration add_user_id_to_albums user_id:integer

albums_controller_user

Then edit the albums controller so that new albums are associated with teh current user.

Next we need to edit our albums model...

album_to_user

Albums belong to user.

And our user model

user_to_albums

User has many albums.

Then we need to make sure that only registered users can edit or add albums. So navigate to the app/views/albums/index.html.erb file and add the following.

add_album

This will make it so that only users who are signed will have access to adding albums.

Then go to the app/views/albums/show.html.erb file and add the following.

user_edit

This will make it so that only users who are signed will have access to editing and deleting albums.

Now lets create a review model and associate it with Albums

The process for creating and associating reviews is the same as with albums and how we associated them with users.

review_association

These are the associations needed for the review model.

album_review_association

These are the associations needed for the album model.

Then we need to create another form partial for the reviews and link it to the show views for albums.

review_partial

This is what the form partial should look like.

review_partial

This is what the form partial should look like.


review_view

Then add this code to the app/views/albums/show.html.erb. This will show the reviews when the user clicks an album.

Let's create a categories model to help filter albums.

To create this model we want to run:

$ rails generate model category name:string

Then we need to create a migration to our database.

$ rails g migration add_category_id_to_albums

Then migrate:

$ rake db:migrate

Then we need to add the appropriate associations to our category model.

category_model

This will associate review with an album and a user.


album_category

This will associate review with an album and a user.


controller_category

Add this into the new method of your album controller

Now we need to make the categories, to do this navigate to the Git Bash Terminal, and run 'rails c' to enter the rails console. And to create our categories by running "Category.create(name: "categoryName")"

rails_console

Adding all of our categories


dropdown

Lastly, we need to add this code to our navbar so we can access the categories with oru dropdown menu.



Almost Done! Lets add the ability for users to attach images to albums.

To do this we need to install ActiveStorage. So in our Git Bash terminal we need to run:

$ rails active_storage:install

albums_image

We need to edit our album model so it has one attached image.


albums_image_form

Then in our form partial for albums we need to add a field to add an image.


albums_image_params

Then edit album_params method so that it requires and album image.


albums_image_show

Then edit album view show.html.erb file to include a place to show images.


albums_image_index

Then edit album view index.html.erb file to show all album images.

And we are all done! I hope you found this review helpful and with it you were able to create a working Ruby on Rails music review site!




Sources

Ruby on Rails Documentation.
BootStrap
RubyGems.org
Devise Gem
Node.js
Yarn
Git Bash