Grant Yoshitsu
5 min readJun 20, 2020

This article is for Ruby beginners.

After an introductory Ruby student learns about arrays, hashes, Object-Oriented Relationships, and Active Record, he or she will next seek to build a web application. If you are at that point in your development, Rails will emerge as your new best friend. This article will discuss the crude basics of designating a premium membership — which will determine whether or not a User receives advertisements on a Rails-built website.

First, you are going to offer the User a choice to determine whether or not he or she is a premium member. On your User’s new and edit pages:

What User sees when visiting /users/new or /users/edit

When they click on the “Yes” box, they will have the option to choose between selecting “Yes” or “No.

To accomplish this, you will use f.select in a form_for with an instance variable (in this case, @user from the User class). To set up the form_for, you will add an instance variable of @user in the User Class’ Controller.

Basic Users Controller that sets an @user instance variable to be utilized in /users/new.html.erb

Since my Rails program is using Active Record and its SQL magic, :is_premium is a column in my users table. In the below example of coding a new and/or edit view page, you can set :is_premium as a string, and pass it along either “Yes” or “No” values via f.select as determined by the User.

form_for in /users/new.html.erb (or /users/edit.html.erb)

Or, in the example below, you can set what the User sees to “Yes” or “No”, while also having the :is_premium attribute receive a value of either true (for “Yes”) or false (for “No”). Your :is_premium attribute would need to correspond by setting its datatype to boolean — instead of a string.

form_for in /users/new.html.erb (or /users/edit.html.erb) with :is_premium set to a boolean datatype

The example below will have :is_premium set to a boolean datatype.

We can proceed by placing an advertisement on the website’s pages - in this specific case, the User’s show page - for all non-premium Users. But, to avoid having the same advertisement run every time the User visits the page, we are going to utilize Cookies and Sessions.

A Cookie is a piece of data that, by being stored on the User’s computer, can be passed back and forth between every single request on a given web page that the User is browsing. A Cookie holds Sessions, which is a large object - more specifically, a large hash - that has been built by Rails. What I am roundabout saying is Sessions is an object used to store data over a short time frame, which it accomplishes by being put into a Cookie.

Okay, so what does this have to do with our code? We are going to use the Session’s hash to store the number of times that the User has visited our show page. In the show action (method) of the Users Controller, we will simply add 1 to the total of session[:view_count] every time that the User visits the page.

Show action in the Users Controller

This particular Rails built application uses a sign-in function for the User’s account. While signed in, the User is saved as the @current_user instance variable. We added a before_action for the current_user method so it can be added to the beginning of every other method in our Rails built application’s Controllers.

Note that :authorized is used to confirm that @current_user exists (is logged in) before allowing the User to access the contents of the website.

The current_user method will be utilized in every Controller that is a child of the Application Controller. In this program, the Users Controller is inheriting from its Application Controller parent.

Note that :authorized’s before action is skipped when creating a new account (via new and create actions). Otherwise, the User would be unable to create an account through which he or she can log into the website.

Therefore, @current_user will be added to the beginning of all actions in the Users Controller. So our show action:

Show action in the Users Controller

Will also have @current_user as an instance variable that can be utilized on the show page.

On our show page, we are going to display the advertisement if the User that is logged in(@current_user) is not a premium user:

/users/show.html.erb. Note that :is_premium is a boolean datatype

We are going to use our session[:view_count] total to alternate between two adds. We accomplish this by using % 2 to determine whether or not session[:view_count] is an odd or even number. How does this work?

% divides the left-hand operator (session[:view_count]) by the right-hand operator (2) and returns the remainder. So session[:view_count] % 2 will return either 1 (if session[:view_count] is odd) or 0 (if session[:view_count] is even).

For the sake of readability, I cut off the code for <iframe width=… These were merely Youtube videos that we embedded into our file. All you have to do is visit a Youtube video, right-click on the video, and choose embed.

Right-click on any Youtube video

And that’s it, you’ve made a Rails-based website that determines the existence of advertisements based upon a User’s premium or non-premium membership.

A premium member does not receive any adds on his or her show page
A non-premium member will receive adds in the form of a photo or youtube video. “THE END” was a part of a Youtube video that appeared on this User’s show page.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response