【问题标题】:Trying to create a new form helper to add a class onto a text field, but it doesn't seem to be adding that class on?试图创建一个新的表单助手来将一个类添加到文本字段中,但它似乎没有添加该类?
【发布时间】:2013-05-07 17:10:41
【问题描述】:

这是助手:

module FormHelper
  def phone_number_tag *args
    args.each do |e|
      if e.is_a? Hash and e.has_key? :class
        e[:class] = e[:class] + " phone_number"
      elsif e.is_a? Hash
        e[:class] = "phone_number"
      end
    end
    text_field_tag args
  end
end

现在奇怪的是,当我把调试器扔进去的时候......

module FormHelper
  def phone_number_tag *args
    args.each do |e|
      if e.is_a? Hash and e.has_key? :class
        e[:class] = e[:class] + " phone_number"
      elsif e.is_a? Hash
        e[:class] = "phone_number"
      end
    end
    debugger
    text_field_tag args
  end
end

...并在控制台中运行text_field_tags args,一旦它命中调试器,我得到:

"<input id=\"_:first___:class___input_span2_phone_number___:placeholder___First__\" name=\"first {:class=&gt;&quot;input span2 phone_number&quot;, :placeholder=&gt;&quot;First&quot;}\" type=\"text\" />"

但是,渲染页面上的 HTML 输出是:

<input class="input span2" id="follow_up_phone" name="referral[follow_ups_attributes][0][phone]" size="30" type="text">

没有phone_number 类,即使在调试器中有。

有人知道为什么吗?

【问题讨论】:

  • 如果你使用“电话号码”作为类会发生什么?
  • @SG86 还是不行

标签: ruby-on-rails ruby forms helper


【解决方案1】:

我认为应该是这样的:

text_field_tag *args

(带星号 *)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-12
    • 2021-12-26
    • 2020-01-21
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 2019-02-27
    • 2014-08-13
    相关资源
    最近更新 更多