Need some information? Theres a form_for that.

As a new student working my way through the magic that is rails, the first thing that really slowed me down was my understanding of how to include basic forms. Through practicing the use of form_for tags and a little bit of research I was finally able to get my head around it a bit better, below are the steps that I followed to create some beautifully basic forms in my quest to become the form master.

The Setup

To create a NEW form we need to first include two actions within our controller that tell our views what information we’ll be passing into the form.

#/app/controllers/appearances_controllerdef new
end
def
create

Within our new action we need to assign an instance variable the value of Appearrance.new:

def new
@appearance = Appearance.new
end

and then write some conditional statements to determine where the page will be redirected to or rendered upon attempting to create.

def create
@apperance = Appearance.new(appearance_params)

if @appearance.save
redirect_to @appearance
#tells the page to redirect to the appearance show page.
else
render :new
#if all information isn't provided reloads the page again.
end

But wait! Whats that appearance_params thing? This is where strong params come into play. For our appearance to take in the appearance_params we need to have knowledge of what columns we have available to us in our schema as well the syntax for writing strong params. They are as follows:

def create
@apperance = Appearance.new(appearance_params)

if @appearance.save
redirect_to @appearance
#tells the page to redirect to the appearance show page.
else
render :new
#if all information isn't provided reloads the page again.
end
private####STRONG PARAM SYNTAX#####
params.require(:appearance).permit(:eye_color, :hair_length, :description)
#the :appearance refers to our controller class.
#:eye_color, :hair_length and :description refer to our table columns.

Now we should be able to make some forms in our view folder! To do so, add another folder within the views folder titled appearances. Within this folder create a new file called new.html.erb (this file acts as our communicator with the new action from our appearance controller file).

Within this file we can add our form_for syntax:

#./app/views/appearance/new.html.erb<%=form_for @customer do |f|%>

<%= f.label :eye_color %><br>
<%= f.text_field :eye_color%><br>
<%= f.label :hair_length %><br>
<%= f.number_field :hair_length %><br>
<%= f.label :description %><br>
<%= f.text_area :description%><br>
<%= f.submit%><% end %>

But what is everything in here doing?

f.label

Creates a label that goes in front of what ever form type you have.

f.text_field

Creates a small text field to input short answer prompts.

f.number_field

Creates a field with arrows that allow you to set a numerical value.

f.text_area

Creates a larger text area for writing longer responses.

f.submit

Adds a button to submit the form.

There are plenty of other helper methods that allow for different functionality, check them out here.

With these tools in your arsenal you’ll be able to create fully functional and diverse forms that allow for a wide variety of outputs.

--

--

--

Budding software engineer based out of Seattle, WA

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Using IAM Authorizer with API Gateway

Connect private OS through bastion OS with an additional feature to be added that is NAT Gateway…

What is Maven?

Let’s learn something about GIT

Fractal Fun: The Cantor Set

What employers look at when evaluating your code

#157: ‘Tohoku Zunko and Friends Special Limited Edition NFT’ Part 1 On Sale Now!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Daniel Patnode

Daniel Patnode

Budding software engineer based out of Seattle, WA

More from Medium

The Unexpected Engineer: How Working with my Neurodivergent Brain Created Magnificent Latent…

Creating a Rails plant tips CMS site

ActiveModel::Serializer & Custom Serialization

Phase 4: We’re So Close