PositivePosts!

Josh Raiborde
5 min readDec 13, 2020

--

This is the blog post for the second project for Flatiron, the Sinatra Portfolio Project

The requirements are as follows. The project needs to use ActiveRecord with Sinatra, use multiple models, use at least one ‘has_many’ relationship on a User model and one ‘belongs_to’ relationship on another model. The project must have user accounts that can sign up, sign in and sign out with the ability to validate the uniqueness of user login attributes, i.e. username or email. Once logged in, a user must have the ability to execute CRUD, which is, Create, Read, Update and Destroy their own resource(s) but not other users resource(s). And the last requirement is to validate user input so that bad data cannot persisted to the update.

My project is a simple web app that posts PositivePosts. Users can post, edit and delete their own posts, all while being able to view other user’s inspirational posts but not be able to edit nor delete their inspirational posts.

In the terminal, type in shotgun and press enter or return. Open Google Chrome and type in localhost:9393 and press enter. You should be directed to the welcome page where you’ll be prompted to either log in or signup.

Let’s sign up for an account!

Let’s go with George Georgie as the First Name and Last Name, george@george.com as the email address, and password as the password. Click Create User and you’re taken to George Georgie’s user page where a flash[:message] (which will disappear if you refresh the page) displays a message congratulating George Georgie for creating a PositivePost Account! This is George Georgie’s user page. There are no PositivePosts listed on George’s page yet because George has posted any PositivePosts yet! We’ll get to that in a minute.

From here, George is prompted to click on one of the links on top of the page to get started. George can logout, View All PositivePosts or Create PositivePost.

Let’s start with the “C”, as in Create, of C.R.U.D., and get George to create a new PositivePost. Click on Create PositivePost and George is taken to a page where he can type in a new short PositivePost.

On the Create a New PositivePost page, George is shown a form to enter in a title and text. George types in “Smile” for the Title and, “Be the reason someone smiles today!” for the Text. George clicks on Submit. And tada! George is taken to the post where he can see that he has just CREATED a PositivePost!

Here, we can see when the PositivePost was created, the Title and Text of the PositivePost and options to either “Edit PositivePost” or “Delete this Post”, as well as go to George Georgie’s PositivePosts. We’ll try out the Edit and Delete function in a bit.

At the top of the page, lets click on the View All PositivePosts to verify that George’s post has been made and that other users can view George’s post! There you go, we’ve verified that George CREATED a post and we’ve also been able to do the “R”, as in Read, of C.R.U.D.! We can also see PositivePosts from other users on this page.

Now, let’s do the “U”, as in Update, of C.R.U.D and get George to update his PositivePost. George clicks on the timestamp of his post, which takes him to that particular PositivePost’s page and then George clicks on Edit PositivePost so he can edit his post.

George adds in “Use your smile to change the world!”, and then clicks the Submit Edit button which takes him back to his page where he view the edit made to his PositivePost post. Let’s verify the edit by clicking on the View All PositivePosts page. And there you go! George has just UPDATED his post!

And now for the “D”, as in Delete, of C.R.U.D. I really don’t want George to delete his post because it’s so good, compared to the other users post (maybe we can get George to create another post after this example), but lets do it.

George clicks on one of his PositivePost and then he clicks on Delete this PositivePost and he’s redirected to the his user’s page where a flash[:errors] (which will disappear if you refresh the page) message displays in red at the top of the page saying, “That post has been deleted”. You’ll notice that post is no longer on George’s user page. If you click on View All PositivePosts at the top of the page, you’ll notice that it is no longer on the index page either.

Awesome! We were able to Create, Read, Update and Delete a post!

Now, let’s see if George can delete another user’s post by clicking on the timestamp of any of the other user’s posts. When George clicks on another user’s post, he’s taken to that user’s PositivePost page and can only view that post, but he is not able to delete that post because that post does not belong to George.

One last thing, lets see if George can post a blank post.

George clicks on Create PositivePost, which takes him to the Create a PositivePost page and clicks on Submit while leaving the Title and Text empty. A flash[:errors] (which will disappear if you refresh the page) message displays in red at the top of the page stating George can’t post an empty PositivePost.

Let’s add in his previous post because it was so good, Be the reason someone smiles today! Use your smile to change the world!”

This web app is a great way to share short PositivePostal posts. Words are powerful and a PositivePost can help people get through some tough times.

Of the many issues, there was this one that I couldn’t figure why, after editing a post the Title would not show up. It turns out that in the edit form, I had “text” as the name when it should’ve been “title” It took a several hours and getting some fresh eyes on it before realizing that I was missing

<textarea name=”title” style=”width:auto; height:auto;”><%= @positive_post.title %></textarea>

from the edit form.

I changed the name=“text” to name=“title” and the title showed up!

One of the other topics I was struggling with, understanding the has_many-belongs_to relationship.

Reading through the ruby on rails guide (https://guides.rubyonrails.org/association_basics.html) helped me understand that using a has_many with the belongs_to relationship.

Here are some key takeaways:

· Because they make common operations simpler and easier in your code.

· Associations are implemented using macro-style calls, so that you can declaratively add features to your models.

· By declaring that one model belongs_to another, you instruct Rails to maintain Primary Key-Foreign Key information between instances of the two models, and you also get a number of utility methods added to your model.

· A belongs_to association sets up a connection with another model, such that each instance of the declaring model “belongs to” one instance of the other model.

· If your application includes authors and books, and each book can be assigned to exactly one author.

· When used alone, belongs_to produces a one-directional one-to-one connection. Therefore each book in the above example “knows” its author, but the authors don’t know about their books. To setup a bi-directional association — use belongs_to in combination with a has_one or has_many on the other model.

· A has_many association is similar to has_one, but indicates a one-to-many connection with another model. You’ll often find this association on the “other side” of a belongs_to association. This association indicates that each instance of the model has zero or more instances of another model.

Because of the belongs_to and has_many relationship, code like this can be used:

<h1><p><%= @positive_post.user.full_name %>’s PositivePost</p> </h1>

and

<% @user.positive_posts.reverse.each do |positive_post| %>

This is a simple app that can help someone in a tough time. I’m glad for this opportunity.

Youtube: https://youtu.be/89qps8D1brE

Github: https://github.com/joshsrai/positive-posts

--

--

Josh Raiborde
Josh Raiborde

Written by Josh Raiborde

Hello, I am a young enthusiastic Software Engineer. Looking forward to an opportunity to use my skills for mutual benefits and growth as well.

No responses yet