Photo by Aleksandr Saenko on Unsplash

Building a Rails App? Yes You CanCanCan!

DJ Taylor
4 min readAug 22, 2021

--

Having moved into the magical land of Ruby on Rails, I’m discovering that my most trusted partner in this journey is Google. The lessons learned through a structured curriculum and classes are great, but in the thick of creating a new app, clues and solutions are everywhere if you know what to search for. As I wrap up my first Rails project, I thought it would be helpful to outline some of the gems I found along the way while building my app.

One aspect of Rails to discuss right off the bat is the amount of work done behind the scenes that enables an app to get off the ground and running with relative quickness and ease. To be sure, this is a beautiful thing, but it can be daunting when trying to navigate it for the first time. I often heard the term ‘automagically’ to describe the things that get abstracted away by the Rails framework. Once you get more familiar with the magic, things really start to flow and you find yourself thanking the universe for such a programmer-friendly setup for building websites. With that said, there are many layers to building an app and that’s exactly where Google comes in handy. Moreover, the wealth of gems others have created in the Ruby community are a huge help for solving challenging problems.

For my Rails app, I wanted to build something that I would actually use in a real world setting and I wanted it to look *decent*. I’m finding that I’m kind of a stickler for design, which occupies its own world of front end development. Anyway, I wanted my app to have at least a little bit of styling built in, which is where the Bootstrap and Bootstrap_Form gems come in. These gems import all of the necessary files into your app and make incorporating style relatively easy both in your views and when building forms. There is plenty of documentation to go along with these as well, which goes a long way when problem solving.

In terms of the actual functionality of my app, I wanted to build something that incorporates sharing both music and stories. As a music teacher, I like to have my students create and share playlists that tell a story that is meaningful to them. Rather than having to pull from various external places (e.g. YouTube, Spotify, Google Docs, etc), my app allows all of that to happen (mostly) in one place. Having played around with incorporating APIs in previous experimental builds, I decided to use the Spotify API (with the help of the RSpotify wrapper) for this app. I’m still very new when it comes to using APIs, but they are great for pulling real world data to seed your database. In my case, I also incorporated a very basic search function that utilizes the Spotify API and returns a limited number of results. Again, the documentation out there is extensive, both on Spotify’s developer site, as well as from numerous other folks who have also used this and other similar services.

Next up, let’s talk about the wide world of third party authentication. For this, I ended up using the Omniauth Google Oauth2 gem, which as the name suggests, uses your Google account for signing up or logging in. While it seems relatively simple to incorporate, landing on which strategy to use (i.e. which third-party provider) can be a bit tricky as implementing each strategy is slightly different. The basic flow is to set up your app with the desired provider, get your personalized key and secret, and then configure the necessary information inside of the config/initializers folder of the application. Storing your third-party keys and secrets is extremely important, though I found making them accessible within my app to be a bit of a challenge. Luckily, the Figaro gem comes in handy here. After installing the gem, Figaro creates a file called ‘application.yml’ in the config folder. From there, you can add the keys and secrets that need to be kept private and easily access them throughout your app by calling ENV['your_secret_key'].

Finally, a key consideration in constructing an app that users interact with is security and authorization. To be sure, this could be its own topic entirely, but in my quest, using the CanCanCan gem definitely saved the day. While custom authorization is always an option, CanCanCan makes this task straightforward. Once the gem is installed, an ‘ability.rb’ file is created in your models folder where you can specify who can do what inside of your app. Whether you have users, admins, or a combination of both, configuring basic authorization is a breeze. The ability model enables you to customize who can manage data (e.g. read, edit, update, create, destroy) versus who can only read or view someone else’s information. Within your app’s views, an extremely useful helper method provided by the gem — if can?— determines what users will see based on the abilities defined in the model. Check out this video for a great intro to setting up CanCanCan, and find the documentation here.

While these gems went a long way in helping build my app, they definitely don’t fully encapsulate the digging and searching I did along the way as I tried to solve problems. As I mentioned before, the guidance provided through Google is invaluable, and frankly, honing my search skills does wonders for quickly being able to adapt and find solutions to problems and bugs that show up throughout the building process. At the end of the day though, it is very cool to realize that answers are out there, you just have to know how to find them. I had a blast building with Rails, and I can’t wait to see what comes next on my journey.

--

--

DJ Taylor
0 Followers

Based in Los Angeles, I’m a full stack developer and recent graduate of the Flatiron School.