以 ajax 呈现的 Rails 3 表单获取与 text_fields 值混合的 html
我以这种方式通过ajax渲染表单:
$('#details').html("<%== escape_javascript(render ('form')) %>");
谢谢到d11wtq! 现在的问题是这个表单是用一些html作为文本来渲染的!尽管文本字段和文本区域中的所有值都是正确的。
这是表单
<%= 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 %>
所以当呈现此表单时,在块 <%= @note.wine.name %> 之后;来红酒div>打印出来。文本字段中包含值和一些 html。文本区域也是如此。插入的html代码总是the some,所以它不是随机的。 感谢您的时间和帮助:)
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 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保原始数据通过,因为一些较新版本的 Rails 目前在转义 java 脚本和 html 时存在问题。还要确保您的选择器就位。并在控制器中响应 js 格式,并在视图中响应 js.erb 模板。
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.