我用这种方式通过ajax渲染表单:
$('#details').html("<%== escape_javascript(render ('form')) %>");
感谢d11wtq! 现在的问题是这个表单是一个渲染,一些html作为文本!虽然text-feild和text-area中的所有值都是正确的。
这是表格
<%= form_for @note, :remote => true, :html => { :'data-type' => 'html', :id => 'new-note-form' } do |f| %>
<% if @note.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@note.errors.count, "error") %> prohibited this note from being saved:</h2>
<ul>
<% @note.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.hidden_field :wine_id, :value=>@note.wine.id %>
<%= @note.wine.name %>
<div class="field">
<%= f.label :rating %> <br />
<%= f.text_field :rating %> </div>
<div class="field">
<%= f.label :text %> <br />
<%= f.text_area :text,:size => "60x12" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
因此,当呈现此表单时,在块&lt;%= @ note.wine.name%&gt;之后来红葡萄酒div&gt;打印出来。在文本字段中有值和一些html。在文本区域也一样。插入的html代码总是一些,所以它不是随机的。 感谢您的时间和帮助:)
答案 0 :(得分:0)
$('#details').html("<%= raw escape_javascript(render ('form')) %>")
确保传递raw是因为某些较新版本的rails目前在转义java脚本和html时遇到问题。还要确保你的选择器到位。并在控制器中响应js formart,并在视图中回复js.erb模板。