Ruby on Rails: How do I add placeholder text to a f.text_field?

Viewed 116922

How can I add placeholder text to my f.text_field fields so that the text comes pre-written by default, and when a user click inside the fields, the text goes away - allowing the user to type in the new text?

7 Answers

In Rails 4(Using HAML):

=f.text_field :first_name, class: 'form-control', autofocus: true, placeholder: 'First Name'

I tried the solutions above and it looks like on rails 5.* the second agument by default is the value of the input form, what worked for me was:

text_field_tag :attr, "", placeholder: "placeholder text"

This way works to me.

<%= form_for @product do |f| %>
<%= f.label :name %>
<%= f.text_field :name, placeholder: "Drill hammer" %>
<% end %>

As you can see, to implement a placeholder, you just can add the "placeholder: "text here", after your text_field name.

Hope my answer can be understood!

Related