将类添加到生成的表单的正确的 button_to 语法是什么?

发布于 2025-01-06 05:35:36 字数 485 浏览 2 评论 0原文

我正在尝试将一个类应用于Rails 3中button_to生成的表单。

:class 选项设置提交按钮的类,因此 docs 告诉我们使用 :form_class 将类应用于表单。

例如,

<%= button_to 'x', user_contact_path(@user, contact), :method => :delete, :form_class => "delete" %>

这只是将属性 form_class="delete" 添加到按钮元素。我尝试了使用 :html_options 等的各种组合。

有人知道该怎么做吗?

I'm trying to apply a class to the form generated by button_to in Rails 3.

The :class option sets the class for the submit button so the docs tell us to use :form_class to apply a class to the form.

E.g.

<%= button_to 'x', user_contact_path(@user, contact), :method => :delete, :form_class => "delete" %>

This just adds the attribute form_class="delete" to the button element. I've tried various combinations using :html_options and so on.

Anybody know how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小霸王臭丫头 2025-01-13 05:35:36

这个方法对我来说非常有效。我能够做到:

<%= button_to "Hello", root_url, :method => :get, :form_class => "my_class" %>

上面生成以下内容:

<form action="http://localhost:3000/" class="my_class" method="get">
  <div><input type="submit" value="Hello"></div>
</form>

但是,这是在 Rails 3.1 中作为您的问题点中的链接,并且同样的内容在 Rails 3.0.x 中不起作用,因为 表单类是硬编码的

来自 url_helper 代码:

("<form method=\"#{form_method}\" action=\"#{html_escape(url)}\" 
  #{"data-remote=\"true\"" if remote} class=\"button_to\"><div>" +
  method_tag + tag("input", html_options) + request_token_tag + 
  "</div></form>"
).html_safe

That method works perfectly fine for me. I am able to do:

<%= button_to "Hello", root_url, :method => :get, :form_class => "my_class" %>

the above generates the following:

<form action="http://localhost:3000/" class="my_class" method="get">
  <div><input type="submit" value="Hello"></div>
</form>

However, this is in Rails 3.1 as the link in your question points and the same wouldn't work in Rails 3.0.x since the form class is hard coded.

From url_helper code:

("<form method=\"#{form_method}\" action=\"#{html_escape(url)}\" 
  #{"data-remote=\"true\"" if remote} class=\"button_to\"><div>" +
  method_tag + tag("input", html_options) + request_token_tag + 
  "</div></form>"
).html_safe
淡水深流 2025-01-13 05:35:36

尝试使用

<%= button_to 'x', user_contact_path(@user, contact), {:method => :delete, :form_class => "delete"} %>

此强制 :form_class => “delete” 成为 options 哈希的一部分,而不是 html_options 哈希。

Try with

<%= button_to 'x', user_contact_path(@user, contact), {:method => :delete, :form_class => "delete"} %>

This forces :form_class => "delete" to be part of the options hash instead of the html_options hash.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文