**任何专家都可以解释为什么在edit.js中使用$("#form > form"),而不是$("#form")?
以下是一些相关代码:**
内部控制器(app/controllers/entries_controller.rb)
def edit
@entry = Entry.find(params[:id])
respond_to do |format|
format.html
format.js
end
end
在 app/views/entries/edit.js.erb 模板中将值设置到表单中,代码如下:
$("#form > form").replaceWith("<%= escape_javascript(render(:partial => "form"))%>")
索引页(app/views/entries/index.html.erb)
<h2>Entry form</h2>
<div id="form">
<%= render :partial => "form" %>
</div>
表单部分(app/views/entries/_form.html.erb)
<%= form_for(@entry, :remote => true) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :address %><br />
<%= f.text_area :address, :rows => 3 %>
</div>
<div class="field">
<%= f.label :phone %><br />
<%= f.text_field :phone %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>