DEV Community

Jesse Franklin Charles Vaughn
Jesse Franklin Charles Vaughn

Posted on

Form_help?

Forms...forms, forms, forms. If you, like me, are just beginning your journey into the land of coding and are already overwhelmed by the fact that there are several different languages within this land, then you're also probably pretty stressed about all these different form syntaxes. I know that I still am and maybe that's why I'm writing this blog in the first place. I mean there's form_tag, form_for, form_with and each of them comes with their own syntax...ughh. Luckily for us though, form_tag and form_for are both soft deprecated and won't be around for much longer. But...I guess until that day we should learn them all right?

Alt Text

Form_tag

The most basic of all the form helpers is the form_tag. A form_tag is what you want to use for a simple form that has just a url path and no model association. When making a form_tag you must specify the url path that you want it to take as well as the http verb.

Note: A substitution tag or expression should be used to enclose each line of your form excluding the end line which should be enclosed in a scriptlet. More documentation on this here

form_tag(url: '/path', method: :get) do 
  label_tag :label
  text_field_tag :label
  submit_tag "Submit"
end
Enter fullscreen mode Exit fullscreen mode

Given you had a rails project set up and a route to '/path' defined, the code above will render a form to the url of /path that has an input box labeled "Label" as well as a button labeled "Submit". Pretty cool right?! Now form_tag accepts more options that I'm not going to get into right now because I'm only beginning to understand them myself but if you'd like to gorge yourself on form_tag here is a very helpful API doc.

Form_for

Now we have form_for which is what you're going to want to use when you have a model that you'd like to associate your form to. Let's say that you have a user model and you want each user to be able to fill out a form. Your form_for would be as follows:

form_for @user do |f|
  f.label :username
  f.text_field :username
  f.submit
end 
Enter fullscreen mode Exit fullscreen mode

This will render a form with a username box and a button labeled "Submit User". Yes! Rails is smart enough to associate the submit button to the model instance we are making the form for. Really cool right?! And that's not even the best form!

Alt Text

Form_with

The one and only form_with was created with the intention of combining both the form_tag and form_for syntaxes into one syntax that can be used in all cases. Whether you have a custom route without a model or a model class built matters not when using form_with. You can specify whether or not you have a model. For example, if you have only a custom route you can use something similar to the code below.

form_with(url: '/path', method: :get) do 
  label_tag :label
  text_field_tag :label
  submit_tag "Submit"
end
Enter fullscreen mode Exit fullscreen mode

This will render the same html and form as the form_tag written above. Say you wanted to use form_with to render the same form that we made with form_for?

form_with model: @user do |f|
  f.label :username
  f.text_field :username
  f.submit
end 
Enter fullscreen mode Exit fullscreen mode

It's easy to see why form_tag and form_for will soon be leaving us as they are no longer needed now that we have form_with. However, I'd say still worth learning about as form_with is built off of them.

Alt Text

Form Helpers

Our forms would be pretty lame without checkboxes, dropdown menus, and the like. That's where form helpers come in for the rescue. Rails with all of its magic comes with several built in form helpers. These will allow us to make our forms more versatile. For instance, say you ran a pet adoption agency and wanted make simple adoption form for a user who wants to adopt a pet. You'd have to somehow have a list of the available pets up for adoption. You could easily achieve this with the collection_select helper.

form_with model: @user do |f|
   f.label :available_pets
   f.collection_select :pets, Pet.all, :id, :name
   f.submit
end
Enter fullscreen mode Exit fullscreen mode

This will render a form with a dropdown menu of all the pets that are in your database. The arguments that are passed to collection_select are first the controller that we will be dealing with(:pets), then the actual collection that we want(Pet.all), after that we to tell the form what attribute we are separating them by so to speak and finally what attribute we want to display in our dropdown menu. This is only one of the many form helpers there are. If you want to see how to use more of them then everything you need to know is here!

Conclusion

Thanks for checking out my very barebones explanation of forms and I hope that it was at all helpful or informative to anyone out there getting started. Also, if there's anything I've written that's incorrect or if you think I missed something critical please let me know. Now get on out there and write some forms!

Top comments (0)