【问题标题】:adding a class to a text_field_tag将类添加到 text_field_tag
【发布时间】:2015-11-25 21:44:10
【问题描述】:

我正在尝试给我的 text_field_tag 上课

我有这个

<%= text_field_tag :login_aei, class: 'form-control' %>

但它不断产生这个:

<input type="text" name="login_aei" id="login_aei" value="{:class=>&quot;form-control&quot;}">

我做错了什么?

【问题讨论】:

标签: ruby-on-rails ruby class form-helpers


【解决方案1】:

你说错了。这是一个可行的方法。

<%= text_field_tag :login_aei, "", class: 'form-control' %>

我将input 字段的值设为空字符串,但您可以给出任何满足您业务需求的值。

【讨论】:

  • 完美运行
【解决方案2】:

在需要数据绑定的情况下,

<%= text_field_tag(:personName,"#{@person.name}", class:'form-control', placeholder: 'User Name' )%>

【讨论】:

    【解决方案3】:

    text_field_tag 是一个 ActionView 方法。首先要做的是check the documentation。它说,text_field_tag 采用这种形式:

    text_field_tag(name, value = nil, options = {})
    

    另外:

    1. value 是文本框的初始值。请注意,默认值为 nil,而不是空字符串。通过传入一个字符串,您以一种未记录的方式使用它,这可能在这种情况下有效,但行为可能略有不同。

    2. name 是一个字符串。虽然 Rails目前在这里将符号转换为字符串,但考虑使用字符串而不是符号以更好地适应未来。

    3. 直接跳到 SO 以获得快速答案,忽略了上述两个问题。考虑 RTFM,因为它是除了源代码本身之外唯一确定的来源。

    【讨论】:

      猜你喜欢
      • 2016-08-22
      • 2014-01-15
      • 2013-11-21
      • 1970-01-01
      • 2014-10-05
      • 2020-10-23
      • 2015-02-10
      • 1970-01-01
      • 2011-12-31
      相关资源
      最近更新 更多