【问题标题】:How do I add an Rails form helper HTML attribute that does not contain a quoted value?如何添加不包含引用值的 Rails 表单帮助器 HTML 属性?
【发布时间】:2014-11-05 02:09:50
【问题描述】:

例如,“必填”。

Rails 在任何尝试添加该属性时都会因此而窒息:

<%= f.email_field :email, :"ng-model" => "whatever", :class => "form-control input-lg", :"required" %>

【问题讨论】:

  • 谢谢,pjmorse。我正要解决这个问题。

标签: ruby-on-rails ruby


【解决方案1】:

只需使用布尔值:

<%= f.email_field :email, :"ng-model" => "whatever", :class => "form-control input-lg", :"required" => true %>

【讨论】:

  • 打败我吧,我刚回来the documentation link
  • 有趣的是,这会在我的 HTML 中输出 required="required"。这是意料之中的吗?
  • 表单助手背后的魔力根据 HTML 标准将 Ruby 常量“true”转换为适当的属性值。看到这个:stackoverflow.com/questions/7089584/…
【解决方案2】:

你必须和你在课堂上做的一样,添加html_attribute =&gt; value

<%= f.email_field :email, :"ng-model" => "whatever", :class => "form-control input-lg", :"required" => true %>

【讨论】:

  • 希望对您有所帮助,:)
猜你喜欢
  • 2014-01-26
  • 2013-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-20
  • 2017-07-08
  • 1970-01-01
  • 2021-02-15
相关资源
最近更新 更多