问题
I'm rendering a form by ajax this way:
$('#details').html("<%== escape_javascript(render ('form')) %>");
Thanks to d11wtq ! Now the problem is that this form is renderend with some html as text! although all the values in the text-feild and text-area are correct.
This is the form
<%= 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 %>
So when this form is rendered, after the block <%= @note.wine.name %> Comes Red Wine div> printed out. In the text-field comes the value and some html in it. And the same in the text-area. The html code inserted is always the some, so it's not random. Thanks for your time and your help :)
回答1:
$('#details').html("<%= raw escape_javascript(render ('form')) %>")
make sure the raw is passed because some newer versions of rails are having problems escaping java scripts and html at the moment. also make sure you selector is in place. And have a respond to js formart in your controller, and a js.erb template in your view.
来源:https://stackoverflow.com/questions/6479048/rails-3-form-rendered-in-ajax-get-html-mixed-with-text-fields-values