我有个很简单的需求,一个大的 form 里面嵌套着个小 form,而小 form 是 ajax 请求 (remote= true)。
试着用 form_tag 写了下,发现生成的 HTML 里面嵌套在里面的小 form 出不来,
<%= form_tag("/search", method: "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<%= form_tag("/searchapp", method: "get", remote:true) do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
<% end %>
生成的 html
<form method="get" action="/search" accept-charset="UTF-8"><div style="margin:0;padding:0;display:inline"><input type="hidden" value="✓" name="utf8"></div>
<label for="q">Search for:</label>
<input type="text" name="q" id="q">
<input type="submit" value="Search" name="commit">
<div style="margin:0;padding:0;display:inline"><input type="hidden" value="✓" name="utf8"></div>
<label for="q">Search for:</label>
<input type="text" name="q" id="q">
<input type="submit" value="Search" name="commit">
</form>
如果改成嵌套 2 个 form 的话,第二个 form 却出来了,但不是在大 form 里面
<%= form_tag("/search", method: "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<%= form_tag("/searchapp", method: "get", remote:true) do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
<%= form_tag("/searchapp2", method: "get", remote:true) do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
<% end %>
生成的 html 如下
<form method="get" action="/search" accept-charset="UTF-8"><div style="margin:0;padding:0;display:inline"><input type="hidden" value="✓" name="utf8"></div>
<label for="q">Search for:</label>
<input type="text" name="q" id="q">
<input type="submit" value="Search" name="commit">
<div style="margin:0;padding:0;display:inline"><input type="hidden" value="✓" name="utf8"></div>
<label for="q">Search for:</label>
<input type="text" name="q" id="q">
<input type="submit" value="Search" name="commit">
</form>
<form method="get" data-remote="true" action="/searchapp2" accept-charset="UTF-8"><div style="margin:0;padding:0;display:inline"><input type="hidden" value="✓" name="utf8"></div>
<label for="q">Search for:</label>
<input type="text" name="q" id="q">
<input type="submit" value="Search" name="commit">
</form>
哪个大神解释下?