Ruby-on-rails 更改由 form_for rails 3.1 生成的 html 表单 id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8682076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:33:05  来源:igfitidea点击:

Change html form id generated by form_for rails 3.1

ruby-on-railsrubyruby-on-rails-3form-for

提问by hyperrjas

I have this form_for:

我有这个 form_for:

<%= form_for [post, Comment.new,], :remote => true do |f| %>
<%= f.text_area :content, :cols =>10, :rows => 1%>
<% end %>
<%= f.submit :class => "input_comment"  %>

That generate the next code html:

生成下一个代码html:

<form method="post" id="new_comment" data-remote="true" class="new_comment" 
action="/post/4efcda9e1d41c82486000077/comments" accept-charset="UTF-8"><div 
style="margin:0;padding:0;display:inline"><input type="hidden" value="?" name="utf8">
<input type="hidden" value="ctVfDF/O4FIR91I7bC5MVezQmutOCkX3dcXe73uNPZY=" name="authenticity_token">

<textarea rows="1" name="comment[content]" id="comment_content" cols="10"></textarea>
<input type="submit" value="Create Comment" name="commit" class="input_comment">
</form>

If I have many forms in a same page is not a html valid with the same id.

如果我在同一页面中有多个表单,则不是具有相同 ID 的有效 html。

  • The id for form_for generate id="new_comment"
  • The id for textarea generate id="comment_content"
  • form_for 的 id 生成id="new_comment"
  • textarea 的 id 生成id="comment_content"

With so many forms in a same page is not valid html.

在同一个页面中有这么多表单是无效的 html。

How can I change the id autogenerate by form_for method helper from rails 3.1?

如何通过 rails 3.1 中的 form_for 方法助手更改 id 自动生成?

回答by Batkins

Adding on to what miked said, the easiest way to make unique form id's for the posts would be to use the post's id numbers in the id attribute, like so:

加上 miked 所说的,为帖子制作唯一表单 ID 的最简单方法是在 id 属性中使用帖子的 ID 号,如下所示:

<%= form_for [post, Comment.new,], :remote => true, :html => { :id => "new_comment_on_#{post.id}" } do |f| %>

回答by Subtletree

I think the :namespaceoption is what you're looking for.

我认为该:namespace选项是您正在寻找的。

It appends the name to the form's id as well asall input and label fields.

它将名称附加到表单的 id以及所有输入和标签字段。

e.g

例如

<%= form_for [post, Comment.new,], namespace: 'NAMESPACE', :remote => true do |f| %>
    <%= f.text_area :content, :cols =>10, :rows => 1%>
<% end %>

Would generate:

会产生:

Form id = NAMESPACE_new_comment

表单 ID = NAMESPACE_new_comment

Textarea id = NAMESPACE_comment_content

文本区域 ID = NAMESPACE_comment_content

From the docs:

文档

:namespace - A namespace for your form to ensure uniqueness of id attributes on form elements. The namespace attribute will be prefixed with underscore on the generated HTML id

:namespace - 表单的命名空间,以确保表单元素上 id 属性的唯一性。命名空间属性将在生成的 HTML id 上以下划线为前缀

回答by miked

You should be able to set the form's id to whatever you want. Something like:

您应该能够将表单的 id 设置为您想要的任何值。就像是:

<%= form_for @object, :html=> {:id => 'custom_form_id'} do |f| %>