Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a default value to text-input in simple-form

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]' %>
like image 636
Mark Avatar asked Aug 08 '12 19:08

Mark


3 Answers

<%= f.input :user, :input_html => { :value => '[email protected]' } %>
like image 197
pgrosslicht Avatar answered Nov 17 '22 12:11

pgrosslicht


You can simply do:

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

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

like image 11
Lesly Revenge Avatar answered Nov 17 '22 11:11

Lesly Revenge


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..

like image 1
Tom Wilson Avatar answered Nov 17 '22 12:11

Tom Wilson