简单形式#input_field方法

时间:2013-06-11 15:29:11

标签: ruby-on-rails simple-form

simple_form 3.0.0.rc上使用rails 4.0.0.rc1的新/未更改安装,此视图代码

<%= simple_form_for @order do |f| %>
  <%= f.input_field :email %>
<%= end %>

产生此输出

<input class="string email optional" id="order_email" maxlength="255" name="order[email]" size="255" type="text" />

但我原本预计输出不会包含maxlength并将type设置为'email',就像#input方法一样:

<input class="string email optional" id="order_email" name="order[email]" type="email" />

我的期望来自simple_form.rb包含以下默认值的事实:

b.use :html5
b.optional :maxlength

我需要做什么才能使input属性从#input_field默认为与#input相同?

3 个答案:

答案 0 :(得分:3)

输入字段辅助方法将在第二个参数中传递给它的哈希值,并将它们转换为信息html属性。看下面的代码,应该做的诀窍:

<%= simple_form_for @order do |f| %>
    <%= f.input_field :email, :type => :email, :maxlength => nil %>
<% end %>

答案 1 :(得分:1)

根据docs input_field方法,除input_html键外,所有选项均为:as, :collection, :label_method, :value_method选项。我尝试添加:as => :email但无济于事。但您可以使用:type => :email在呈现的html中获取type="email"。根据方法的来源,它也使用了一些默认值。

所以要获取电子邮件字段:

<%= simple_form_for @order do |f| %>
  <%= f.input_field :email, :type => :email %>
<% end %>

答案 2 :(得分:0)

我认为它与你的数据库字段有关...我认为你已经将数据库字段设置为字符串,一般有255个最大长度。这可能是它自动添加255的原因?