表中的 fields_for 生成技术上不正确的 HTML
当我使用如下构造时:
<table>
<%= f.fields_for :group_locations do |gl| %>
<tr>
<td><%= gl.label :group_id %></td>
<td><%= gl.select :group_id, paths %></td>
</tr>
<% end %>
</table>
我在浏览器(Safari)中针对表中的每一行出现错误( 不允许在 内使用。在 之前插入 ; 相反。)这是由于关联 ID 的隐藏
放置在
之后造成的。如何使 id 的
出现在 TD 元素之一内?
When I use a construct like:
<table>
<%= f.fields_for :group_locations do |gl| %>
<tr>
<td><%= gl.label :group_id %></td>
<td><%= gl.select :group_id, paths %></td>
</tr>
<% end %>
</table>
I get an error in my browser (Safari) for each row in the table (<input> is not allowed inside <tbody>. Inserting <input> before the <table> instead.
) This is caused by the hidden <input>
for the association's id being placed after the </tr>
. How can I cause the id's <input>
to appear inside one of the TD elements?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为如果您手动打印隐藏字段将不会被打印。你能试试这个吗?
I think the hidden field won't be printed if you print it manually. Could you try this?
fields_for
方法将隐藏 id连接到它捕获的块的末尾。因此,如果您将
<% end %>
标记放在第二个之前,您应该会得到您想要的结果。
The
fields_for
method concatenates the hidden id<input>
to the end of the block it captures. So, if you put your<% end %>
tag before your second</td>
, you should get the result you want.这更多的是评论(但我似乎无法直接评论)。只是想指出 Dogbert 的答案对我有用。大多数浏览器似乎并不担心格式错误的 html...直到我尝试在 IE8 中的表格上使用 jquery ui sortable,这导致了许多问题(包括崩溃)。无论如何,将 id 显式包含在 td 内的隐藏字段中似乎是可行的方法。您可能只想考虑仅在对象持久存在时才执行此操作(否则隐藏字段没有值,这可能是也可能不是问题,具体取决于您的代码)。添加检查以查看对象是否持久化可能如下所示(在上述情况下):
This is more of a comment (but I don't seem to be able to comment directly). Just wanted to note that Dogbert's answer worked for me. The malformed html didn't seem to worry most browsers... until I tried to use jquery ui sortable on the the table in IE8, which caused a number of problems (including a crash). Anyway, explicitly including the id in a hidden field inside a td seems the way to go. You may want to consider only doing this if the object is persisted (otherwise the hidden field has no value, which may or may not be an issue depending on your code). Adding a check to see if an object is persisted might look like (in the above case):