cdm89
cdm89

Reputation: 165

How do you add text inside a text field in Rails?

I would like to add username inside the username text field in my application (vs having a label next to it) the user would be able to type over this text. I've seen it done in html but I'm confused as to how to do it on my new.html.erb page since im using the form_for tag and the text_field tag.

thanks in advanced!

Upvotes: 0

Views: 1590

Answers (2)

Jason Kim
Jason Kim

Reputation: 19031

Use "placeholder".

<%= form_for @user do |f| %>
  <%= f.text_field :username, :placeholder => "Username" %>

  ...

  <%= f.submit %>
<% end %>

This will only work for html5 supported browsers by the way, so make sure you have jquery fallback for IE 8, 7 etc.

Upvotes: 1

AlexBrand
AlexBrand

Reputation: 12399

This is achieved by using the HTML5 placeholder attribute.

In rails, you can simply add the placeholder attribute:

<%= f.text_field :whatever, :placeholder => "text for placeholder..." %>

Upvotes: 0

Related Questions