使用 Rails 的 form_for 但在 <form> 上设置自定义类和属性元素? [英] Use Rails’ form_for but set custom classes, attributes on &lt;form&gt; element?

查看:36
本文介绍了使用 Rails 的 form_for 但在 <form> 上设置自定义类和属性元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

form_for 似乎忽略了任何额外"属性,例如作为 options 传递的 data-foo 属性或 class> 在它的第二个参数中.

form_for seems to ignore any 'extra' attributes like a data-foo attribute or class passed as options in its second argument.

= form_for @user, {:url => 'foo', :class => 'x', 'data-bar' => 'baz' } do |f|
  # ...

输出是一个

标签,没有 x 类或 data-bar 属性.

The output is a <form> tag with no x class or data-bar attribute.

解决方法是什么?

或者,如何在不使用 form_for 的情况下获取 FormBuilder 实例?

Or, how can I grab a FormBuilder instance without using form_for?

推荐答案

使用 :html 哈希:

= form_for @user, :html => {:class => 'x', 'data-bar' => 'baz'} do |f|

= form_for @user, html: {class: 'x', data: { bar: 'baz' } } do |f|

这篇关于使用 Rails 的 form_for 但在 <form> 上设置自定义类和属性元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆