Ruby-on-rails 以简单形式向文本输入添加默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11871827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:50:10  来源:igfitidea点击:

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

ruby-on-railsruby-on-rails-3simple-form

提问by Mark

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

我想使用 simple-form 向文本输入字段添加默认值。有了:placeholder它不作为默认....

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

回答by pgrosslicht

<%= f.input :user, :input_html => { :value => '[email protected]' } %>

回答by Lesly Revenge

You can simply do:

你可以简单地做:

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

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

text_field如果您正在使用像 Ransack 这样的表单搜索 gem,那就太好了。

回答by Jeni

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

在 Rails 5.1 上placeholder: 'aaaaaaaaaaa'工作。例如

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

will work on rails 5.1

将在 Rails 5.1 上工作

回答by Tom Wilson

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

您可以在控制器中执行此操作,并将数据详细信息保留在表单之外。取而代之的是: 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

你可以这样做: 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..

“新”表单将打开并填充默认(预设)值。这应该适用于简单表单、普通旧 Rails 或任何其他以 Rails 方式执行操作的表单生成器。

回答by Kirill Zhuravlov

You can try this option:

你可以试试这个选项:

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