使用Rails的form_for但设置自定义类、元素上的属性?

时间:2021-02-04 15:56:40

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

form_for似乎忽略了任何“额外”属性,如data-foo属性或类,在第二个参数中作为选项传递。

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

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

输出是一个没有x类或数据条属性的

标记。

What’s the fix?

解决办法是什么?

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

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

5 个解决方案

#1


158  

Use the :html hash:

使用:html散列:

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

Or

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

#2


11  

Rails 4.0.3, Ruby 2.1.0p0 -> this worked for me =>

Rails 4.0.3, Ruby 2.1.0p0 ->这对我来说是有用的=>

<%= form_for(@contact, :html => {:class => 'form_height'}) do |f| %><% if     @contact.errors.any? %>

#3


4  

I had the same problem but was puzzled that another form elsewhere in my app was working fine.

我也遇到了同样的问题,但我很困惑,我的应用程序中其他地方的另一种表单运行得很好。

I realized that I had accidentally added a form_for inside another form_for which once removed cured the problem.

我意识到,我不小心在另一个form_for中添加了一个form_for,这个form_for一旦删除,问题就解决了。

Secondly, I should add that this syntax works for me in Rails 4.2:

其次,我要补充的是,在Rails 4.2中,这种语法对我是适用的:

<%= form_for @type, html: {class: "form-horizontal"} do |f| %>

I find it preferable to the punctuation-soup of the other answers here (which were perhaps based on an older Rails version).

我发现它比这里的其他答案(可能是基于旧的Rails版本)更可取。

#4


1  

On mostly helpers, the last arg is a hash of html options for the element.

在大多数helper类中,最后一个arg是元素的html选项的散列。

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

You can also check other alternatives in the documentation ActionsView::Helpers::FormHelper

您还可以检查文档ActionsView中的其他选项::helper::FormHelper。

#5


0  

I tried the above with no luck but found a solution. I'm using rails 4.1.6.

我尝试了上面的方法,但没有运气,但找到了一个解决方案。我使用rails 4.1.6。

This didn't work

这没有工作

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

This did

这并

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

notice the difference with the html option, hope this helps

注意html选项的不同之处,希望这能有所帮助

#1


158  

Use the :html hash:

使用:html散列:

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

Or

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

#2


11  

Rails 4.0.3, Ruby 2.1.0p0 -> this worked for me =>

Rails 4.0.3, Ruby 2.1.0p0 ->这对我来说是有用的=>

<%= form_for(@contact, :html => {:class => 'form_height'}) do |f| %><% if     @contact.errors.any? %>

#3


4  

I had the same problem but was puzzled that another form elsewhere in my app was working fine.

我也遇到了同样的问题,但我很困惑,我的应用程序中其他地方的另一种表单运行得很好。

I realized that I had accidentally added a form_for inside another form_for which once removed cured the problem.

我意识到,我不小心在另一个form_for中添加了一个form_for,这个form_for一旦删除,问题就解决了。

Secondly, I should add that this syntax works for me in Rails 4.2:

其次,我要补充的是,在Rails 4.2中,这种语法对我是适用的:

<%= form_for @type, html: {class: "form-horizontal"} do |f| %>

I find it preferable to the punctuation-soup of the other answers here (which were perhaps based on an older Rails version).

我发现它比这里的其他答案(可能是基于旧的Rails版本)更可取。

#4


1  

On mostly helpers, the last arg is a hash of html options for the element.

在大多数helper类中,最后一个arg是元素的html选项的散列。

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

You can also check other alternatives in the documentation ActionsView::Helpers::FormHelper

您还可以检查文档ActionsView中的其他选项::helper::FormHelper。

#5


0  

I tried the above with no luck but found a solution. I'm using rails 4.1.6.

我尝试了上面的方法,但没有运气,但找到了一个解决方案。我使用rails 4.1.6。

This didn't work

这没有工作

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

This did

这并

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

notice the difference with the html option, hope this helps

注意html选项的不同之处,希望这能有所帮助