使用 jQuery 将 Rails 表单加载到表中
这已经让我的项目死了几天了。
我有一个标准的价值观表;框架如下:
<table><tbody><tr><td>Stuff!</td></tr></tbody></table>
我正在尝试使用 jQuery 将内联 Rails 3 表单加载到 tbody
中,让用户向表中添加新值。这是通过一个按钮完成的,该按钮获取控制器的新操作,然后调用 new.js.erb,它只是将表单渲染的部分添加到表中 ($("table tbody").prepend(' <%= escape_javascript(render 'form') %>');
)。
表单部分本身是这样的:
<tr>
<td>
<%= form_for @model, :remote => true do |f| %>
</td>
<td>
<%= f.text_field :column %>
</td>
<td>
<%= f.submit "Add" %>
<% end %>
</td>
</tr>
这就是混乱开始的地方:在 Webkit 浏览器和 IE 中,这按预期工作!生成的 HTML 是这样的:
<table>
<tbody>
<tr>
<td>
<form>
</form>
</td>
<td>
<input>
</td>
<td>
<input type="submit">
<td>
</tr>
<tr>The list of values</tr>
</tbody>
</table>
当然,事实上,在输入开始之前,表单在同一个单元格中开始和结束,这应该会引发一个危险信号,表明出现了问题。但是,该表单仍然可以提交并且在其他方面工作正常。
在 Firefox 中,情况并非如此。表单呈现的内容更像是这样的(显示的只是前置行中的内容,因为其他内容都是相同的):
<td>
<form>
</form>
<td>
<input>
</td>
<td>
<input type="submit">
<td>
</td>
这里的区别在于,与 Webkit/IE 浏览器不同,Firefox 将所有内容都放入 form
中标签的单元格。此外,该表单根本不起作用 - 它甚至不提交。甚至没有发出任何请求。
现在,从我的一些研究中,我了解到我在这里可能犯了一个巨大的错误,而差异来自于浏览器尝试以不同的方式呈现我遗漏的任何内容。通过尝试,我发现这与 Rails 表单如何与表格交互没有什么关系 - 如果您只渲染表格内的部分,则浏览器之间的 HTML 是相同的(表单标签仍然在其他输入之前关闭)但是,这可能意味着我将 <% end %> 放在了错误的位置!)。当我使用 jQuery 预先渲染渲染的部分时,发生了一些错误:可能由于没有封闭的表标签而导致部分渲染错误,然后插入到表中,从而导致问题。
真的,我已经被难住了。我已经广泛地寻找了这个问题,虽然有一些无关紧要的问题,但似乎没有一个真正触及我的问题。
我预计我犯了一些非常简单的错误 - 但它是什么?
任何帮助将不胜感激!我希望我已经尽可能清楚地解释了这个问题。
This is something that has killed my project dead for a few days now.
I have a standard table of values; with a skeleton something like this:
<table><tbody><tr><td>Stuff!</td></tr></tbody></table>
I am trying to load an inline Rails 3 form into the tbody
using jQuery to let the user add new values to the table. This is done with a button which gets the new action of my controller, which in turn calls new.js.erb, which simply prepends the form's rendered partial into the table ($("table tbody").prepend('<%= escape_javascript(render 'form') %>');
).
The form partial itself goes something like this:
<tr>
<td>
<%= form_for @model, :remote => true do |f| %>
</td>
<td>
<%= f.text_field :column %>
</td>
<td>
<%= f.submit "Add" %>
<% end %>
</td>
</tr>
Here's where the confusion begins: in Webkit browsers and IE this works as expected! The resulting HTML goes something like this:
<table>
<tbody>
<tr>
<td>
<form>
</form>
</td>
<td>
<input>
</td>
<td>
<input type="submit">
<td>
</tr>
<tr>The list of values</tr>
</tbody>
</table>
Of course, the fact that the form begins and ends in the same cell, before the inputs begin, should raise a red flag that something is going wrong. However, the form still submits and works perfectly otherwise.
In Firefox, this is not the case. The form renders something more like this (shown is just the stuff inside the prepended row since everything else is the same):
<td>
<form>
</form>
<td>
<input>
</td>
<td>
<input type="submit">
<td>
</td>
The difference here is that unlike the Webkit/IE browsers, Firefox throws everything into the form
tag's cell. Also, the form does not work at all - it does not even submit. No requests even get sent out.
Now, from some of my research I learned that there is probably a huge mistake I am making here and the difference comes from the fact that browsers try to render whatever I've left out in different ways. From playing around with it, I've figured out that this has little to do with how Rails forms interact with tables - if you just render the partial inside a table the HTML is identical between browsers (the form tag still closes before the other inputs begin, though, which may mean that I'm putting <% end %> in the wrong place!). Something is happening wrong when I am prepending the rendered partial using jQuery: perhaps the partial renders wrong due to not having enclosing table tags and is then inserted into the table, causing the problems.
Really, I have been stumped. I have looked for this far and wide and while there are some tangentially related questions none seem to really hit on my issue.
I am expecting that I've made some very simple mistake - but what is it?
Any help would be GREATLY appreciated! I hope I've explained the problem as clearly as possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看局部就很明显了。你有一个嵌套错误。如果您在某个元素(在本例中为表格单元格)内打开表单标签,您也需要在那里关闭它。在这种情况下验证标记会有所帮助。
如果您必须使用表格,您可以在其周围放置表单标签并将完整内容插入到页面中。
By looking at the partial it is quite obvious. You have a nesting error. If you open a form tag inside some element (in this case table cell) you need to close it there too. Validating the markup helps in this kind of cases.
If you must use table, you could put form tags around it and insert the full thing to the page.