35

I want to add a default value to a text-input field using simple-form. With :placeholder it is not used as default....

<%= f.input :user, :placeholder => '[email protected]' %>
2
  • to have default value and also keep actual value in field (for example user's email) try like this --- f.input :user, input_html: { value: @user.email.present? ? @user.email : '[email protected]' }. Also its new more readable syntax in RoR Commented Jun 9, 2016 at 18:02
  • I think what you want is something like value: ( f.object.user.presence || '[email protected]' ), which will use the existing value or set it to [email protected] if nothing is set. This will also handle empty Strings "" and consider that nil and use the default value. Commented Sep 19, 2024 at 16:51

5 Answers 5

64
<%= f.input :user, :input_html => { :value => '[email protected]' } %>
Sign up to request clarification or add additional context in comments.

2 Comments

Maybe, :input_html => { :value => '[email protected]' }
Yes, realized that too. Fixed it.
12

You can simply do:

<% f.text_field, value: '[email protected]' %>

text_field is good if you are working with form search gem like Ransack.

1 Comment

I think if you do not use the :input_html option and simply use :value or value: then that value will be reset to the default each time you return to the edit page.
3

You can do this in the controller and keep data details out of your forms. Instead of this: def new @article = Article.new end

you can do this: def new # hardcode default values (as shown) or generate on the fly @article = Article.new(title: "10 Best Things") end

The "new" form will open with the default (pre-set) values filled in. This should work with simple-form, plain old Rails, or any other form generator that does does things the Rails way..

Comments

2

On rails 5.1 placeholder: 'aaaaaaaaaaa' works. E.g.

<%= f.input :user, :placeholder => '[email protected]' %>

will work on rails 5.1

Comments

1

You can try this option:

<%= f.input :user, label: '[email protected]' %>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.