How to Give Genuine Recognition

Recognition is one of the most powerful tools that any leader has in their arsenal. The desire to be recognised is a basic human need; we all want to be noticed for our individual efforts…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Ruby on Rails and How to Fight the Nested Resources Boss

Originally submitted in July 2020. This article is my Ruby on Rails project blog. I hope it is helpful and inspiring for you.

Yes, this is how I called my Rails Project Blog title. So it feels like that, like getting to the final boss and having a hard time, like a good old video game.

Dr. Wily and everyone I had to choose to confront before him are my childhood’s worst enemy.

For the Rails project, I decided to go ahead and continue with the Small Reviews project. In case this is the first blog post you read from me, Small Reviews is my real-life movie review project. One of my goals in life is Small Reviews (take from the Spanish word reseñita, kind of I would say pequeña reseña rather than reseña in Puerto Rican slang) turning into a website or app for people who share the same passion as me.

In this project, I decided to create the entire CRUD structure using gems Devise for authentication, Omniauth for Facebook users to log in (I decided on Facebook cause I want this to be as close as the real Go-Live thing as possible). There are many gems to choose from. Pick and use them accordingly but be careful not to get too greedy or addicted to them.

After creating the users and allowing them to log on, I proceeded to add the reviews and categories tables to start talking about our favorite movies and categorize them by genres. To add a Nested Resource, what better than adding comments? Like an author getting away with lazy writing by killing a character, I chose to simplify the project by just adding comments. After all, this is all we do on the Internet, comment about anything, right?

Well, nested resources will always feel like a labyrinth. Is scary, but I will get this done. First, let see the review and comment models.

The reviews will belong to the user, to a category, and will have many comments as well as many users through comments. The comments will belong to the user who wrote it and the review in it is written. Now, let’s take a look at the controllers.

Reviews Controller:

Comments Controller:

Now that we’re done with the controllers, let’s work with the code in the views. This is how the comment-related codes will look like in our show review file.

If the user is signed in, he’ll be able to see the comment form rendered from file /views/comments/form.

Here’s the code from that helpful file.

As you can see, that form is iterating the errors and everything. Pretty cool.

Now that we have the form, let’s show those passionate film connoisseurs' comments.

Now that we have the form, let’s show those passionate film connoisseurs’ comments.

Here, we are stating that reviews will be shown if they’re created. We don’t want that Review page to look ugly with Comments: header with nothing. So we are rendering from a partial file in the views/reviews directory. Let’s take a look at how we are iterating the data from that file.

The last touch I gave is another statement. If the user is signed in and is the one who wrote the comment or an admin, the user will have access to either edit it and delete it. A good friend of mine showed me how to create that method in the Usermodel and I’ll pay it forward by showing it to you.

Pretty neat, huh? Finally, let’s do the most important thing when creating the nested resource. The one thing that made that resource….nested. Let’s go to the routes file and add the following.

You can even modify it by adding only:[:show, :new, :create, :edit, :update, :destroy] however you like or what your project requires to function as expected.

Code line by code line, you’ll finally be able to beat the boss. There’s no guarantee the princess will be in that castle.

Add a comment

Related posts:

Everything is an object in Python

Now is time to talk about one of the most used languages in programming and also one of the easiest to learn due to its simple syntax. The content of this post is meant to be technical, as I will…

Six Reasons Why You Should Write For Yourself

Writing is an art. It may be a hobby to some, a profession to some, or an ardent source of joy and passion to some. With digital marketing, SEO and content-based writing on the boom, the art of…

The Wishful Start

I believe we attain what we attract and if we call upon happiness, wishing ourselves and the world well, that is exactly what we shall receive. So based on my today and what I plan for tomorrow, I…