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

好久不见. 提交于 2019-11-29 16:01:47

问题


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 => 'user@domain.com' %>

回答1:


<%= f.input :user, :input_html => { :value => 'user@domain.com' } %>



回答2:


You can simply do:

<% f.text_field, value: 'user@admin.com' %>

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




回答3:


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

<%= f.input :user, :placeholder => 'user@domain.com' %>

will work on rails 5.1




回答4:


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




回答5:


You can try this option:

<%= f.input :user, label: 'user@domain.com' %>


来源:https://stackoverflow.com/questions/11871827/adding-a-default-value-to-text-input-in-simple-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!