更改由form_for rails 3.1生成的html表单ID [英] Change html form id generated by form_for rails 3.1

查看:64
本文介绍了更改由form_for rails 3.1生成的html表单ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此form_for:

I have this 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"  %>

生成下一个html代码:

That generate the next code 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>

如果我在同一页面中有很多表单,则不是具有相同ID的html有效文件.

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

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

在同一页面中有这么多表格是无效的html.

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

如何更改来自rails 3.1的form_for方法助手自动生成的ID?

推荐答案

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

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:

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

这篇关于更改由form_for rails 3.1生成的html表单ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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