Helpers for form elements

Checkboxes

<%= check_box_tag(:pet_dog) %>
<%= label_tag(:pet_dog, “I own a dog”) %>
<%= check_box_tag(:pet_cat) %>
<%= label_tag(:pet_cat, “I own a cat”) %>

This will generate the following html

<input id=”pet_dog” name=”pet_dog” type=”checkbox” value=”1" />
<label for=”pet_dog”>I own a dog</label>
<input id=”pet_cat” name=”pet_cat” type=”checkbox” value=”1" />
<label for=”pet_cat”>I own a cat</label>

Radio Buttons

<%= radio_button_tag(:age, “child”) %>
<%= label_tag(:age_child, “I am younger than 18”) %>
<%= radio_button_tag(:age, “adult”) %>
<%= label_tag(:age_adult, “I’m over 18”) %>

This generates the following HTML

<input id=”age_child” name=”age” type=”radio” value=”child” />
<label for=”age_child”>I am younger than 18</label>
<input id=”age_adult” name=”age” type=”radio” value=”adult” />
<label for=”age_adult”>I’m over 18</label>

Text Area

To create a larger text box, it is recommended to use the `text_area_tag`

<%= text_area_tag(:message, “This is a longer text field”, size: “25x6”) %>

This will create the following HTML

<textarea id=”message” name=”message” cols=”25" rows=”6">This is a longer text field</textarea>

Number Field

This will create an `input` element

<%= number_field :product, :rating %>

To specify a range of values, we can use the `in:` option

<%= number_field :product, :rating, in: 1..10 %>

Password Field

Sometimes you want the characters typed by the user to be masked. This will generate an ``

<%= password_field_tag(:password) %>

Email Field

This will create an ``

<%= email_field(:user, :email) %>

Telephone Field

This will create an ``.

<%= telephone_field :user, :phone %>

Date Helpers

  • `input[type=”date”]`
    <%= date_field(:user, :reservation) %>
  • `input[type=”week”]`
    <%= week_field(:user, :reservation) %>
  • `input[type=”year”]`
<%= year_field(:user, :reservation) %>
  • `input[type=”time”]`
<%= time_field(:user, :check_in) %>

--

--

--

Trusted Veteran | Compassionate. Aspiring. Resourceful.

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

Recommended from Medium

Java Data Structures

Printronix RFID-Label-Printer

Recovering your data from a Seagate Central with Windows 10

Hackster’s Handpicked Projects of the Week

Controlling Infrared Devices Remotely with Your Browser

images/ir_proxy_architecture.png

Git branches

Multi-Tenant Google Cloud Platform B2B SaaS Applications How-to

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
Austin Songer

Austin Songer

Trusted Veteran | Compassionate. Aspiring. Resourceful.

More from Medium

Why should you use Devise gem for Rails authentication?

Construyendo una API RESTfull JSON con Rails 6 [1ra Parte]

JavaScript’s OOP English-isms

Dealing with rich text in Rails 7