How To Create A Form In Rails

Get in Formation

As I have explained in previous articles, the Rails framework utilizes a methodology commonly referred to as convention over configuration. What this means is that as long as you adhere to the built in assumptions that come with the rails framework you will have access to a host of helpers that will make your life much, much easier.

One of the ways we like to use helpers is when it comes to building forms. I am sure you are familiar with the way we created forms in the past – they may look something like this:

forms1.png

We can see how we start by opening up with the form tag and inside of it we need to declare two things: action and method.

action='/somepath' is responsible for redirecting all of the data from the form (stored in something called params – a hash) to a new url path (in this case /somepath).

method='post' is defining the type of http request to make, in this case it is a POST which will take data from the user and persist it in our database.

This is great and all, but we are on to bigger and better things now. Sinatra was kid’s play, let’s pull out the big guns now. From now on, when we want to create a form we are going to be using the rails helper, form_for. All of this will be done in the new.html.erb file which is located in the views folder. Here’s how it’s done:

form2.png

In the past, we needed to explicitly type out everything for input, label and form among others, but form_for will be doing that for us with the use of ERB.

A word of caution, on first glance and practice you may feel that this method of form creation is counter-intuitive but stick with it as it is a more efficient use of time and will result in DRYer code in the long run.

By using ERB we can leverage the use of logic to create our form. The syntax for this form is as follows: Anything that is logic or otherwise not being printed to the screen is opened and closed with ERB tags (<% ... %>) and anything being printed to the screen will have the same with the addition of an equal sign on the opening tag, like so – (<%= ... %>).

From there, we need to state what this form is being used for. You will notice the line form_for(book). The (book) in this case is actually pointing to an instance variable @book which is defined in the controller from the books_controller.rb file. Let’s take a look:

form4.png

The rails magic is at play here, as it intuitively knows to use the new method, which then gives way to the create method (the create method is what will persist the information to the database)

Once our view file is run (new.html.erb), the ERB code from our form_for will be evaluated and then be rendered as pure HTML. It should look something like this:

form 6.png

This is wild! Look at how little code it took to create all that, this is a force multiplier for sure! For those wanting to know what is happening under the hood, the resulting data that is inputted from the user is bundled up into a params hash (like we have used in the past with Sinatra). That hash is then sent out as a POST request to the controller, which uses the create method, which in turn persists the user’s data to the database!

Check out these additional resources for further clarification:

Submit a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s