Rails 3 form rendered in ajax get html mixed with text_fields values

对着背影说爱祢 提交于 2019-12-12 04:13:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!