Its a Rails App Baby. Using form_for to cast magic.

Billy Martin
3 min readFeb 8, 2021

Hey ya’ll. If you are like me, and have started using rails, then you probably feel like the picture above. You have started to harness the magic that is rails and are taking advantage of all of the tools that are now at your fingertips. Its no joke. Half the stuff rails does for us makes it feel like an actual magician lives inside our keyboard. If you don’t feel that way, well then maybe we just see the world a bit differently. Lets talk about a particular bit of sorcery that rails has in it’s spell book.

Look at that bomb a$$ code right there. Gosh dang. You know how much html you can fit under that baby? That’s right kids, these few lines of code here happen to be writing like 4235 lines of html for us. No I’m serious. like 4 million lines of code! … Okay but for real, using the form_for magic that rails hands us on a silver platter, we can easily and understandably create forms for our views. Let’s eagle eye this $hit and take a closer look at what’s going on here.

Daaang. Look at all that code we didn’t have to write. Feels good man. Let’s break this down a bit shall we? First we have our form. It’s given the class of “new_comment” with an id of the same, and is routing to our “/comments” action. This is some rails magic that uses associations and the .build method to create a comment for this specific climb. Second you’ll see that we have two hidden_field tags that allow us to slam some predetermined parameters into our form. Next comes the Label tag. This just creates a label for us so we know what info we are wanting from the user. Then the text_area tag gives us a place for our user to give us that sweet sweet comment. Easy cheesy right? Finally you’ll notice we have our submit input. We literally typed f.submit and it gave us like 74 words. Sheesh.

There you have it. A super basic spell that you can cast anytime you’re using rails and want to write forms without actually writing html. VERY COOL.

--

--