Refactoring the Comment Form into a Partial and Implementing Data Validations
In this guide we're going to perform some housecleaning. Specifically, we're going to refactor our comment form so that it is called from a partial instead of the blog show page, additionally, we're going to integrate a number of validations for creating comments.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this guide we're going to take a little bit of a step back. Before we dive into all of the JavaScript and Jquery we were going to have to write in order to get this working. We are going to clean up some of our rails code. I am really not a fan of having our entire show action or a show template for a blog taken up with this comment form in fact about half the lines of code are for a pretty basic comment code and comment form that I don't think it deserves. So I'm going to cut that out and let's create a partial. So I'm going to say...

<%= render 'comments/comment_form' %>

And now if I hit save. All we have to do is create a comment form so if you go to app/views/comments and create a new file. Make sure you start it off with an underscore and I'll say...

_comment_form.html.erb

Paste all of that in

large

Now if we hit save all of this should still work. Now if I come back here hit refresh. There we go. We still have access to our content form. If I hit inspect all of our cool form for stuff should still work. So if I come to form it has this new_comment ID and name and class it has our action. So everything is getting picked up properly, so that's good. And this looks much better, from a code perspective.

Now that we have this, one thing that would not be good is right now we allow all kinds of various options for creating a comment. For example, we would allow a comment to be created without any content. That would be a bad idea. Let's put some validations in place. I'm going to come to our comment model and now let's create a 'validates method' for our content attribute. We're first going to say...

validates :content, presence: true, length: { minimum: 5, maximum: 1000 }

The presence has to be true and then we're going to dictate the length. Length takes a hash because we can give this a range and it takes {} inside and we can say the minimum is going to be 5 and the maximum is going to be 1000 and this is characters, so in other words you want them to write at least five characters you could even move this up. You could say that we don't want any little spammy comments so we could have 10 characters, something like that. I'll put it at 5, maximum as 1000 because you don't want people putting a gigantic amount of content in there. Hit save and that is all we need to do now. To verify to make sure it's working. Come into the terminal and let's say...

rails c --sandbox 

just open up in sandbox mode so that we don't create anything real here. If I do...

Comment.create!(user_id: User.last.id, blog_id: Blog.last.id)

and no content. This should now give us a validation and it does it says ActiveRecord: :RecordInvalid: Validation failed: Content can't be blank, Content is too short (minimum is 5 characters) Notice here these are two errors. So let's try this again. And now let's pass in content but only pass in two characters. So if we do this...

Comment.create!(user_id: USer.last.id, blog_id: Blog.last.id, content: "12")

we still get an error. But now, oh this one we got because it is a syntax error. Run it again and now we get another error but it's now shrunk. Now it is only saying that content is too short and it even gives you a cool error that says minimum is 5 characters so our validations are working nicely.

git status 

See what we changed.

git commit -m "Refactored form into partial for comments and added validations to comment model" 

Now if you

git push origin actioncable 

This is all going to get pushed up and we are going to be good to go. Now brace yourselves in the next guide. We're going to get into some of the very fun but very challenging parts about using ActionCable. Especially if you don't have a lot of Javascript experience. Don't let what we're going to go through intimidate you at all if Javascript isn't your favorite. Hopefully, afterwards you can see some of the power that implementing Javascript on top of a Rails application gives you, and you'll be able to use it in your future apps.

Resources