【发布时间】:2009-08-13 19:36:52
【问题描述】:
好的。我正在使用 RoR 和 jQuery,并且我已经获得了更新并创建了与他们的 .js.erb 文件一起工作和运行的链接。使用这行代码创建时会出现问题。
#create.js.erb
$("#promo_types").append("<%= escape_javascript(render(:partial => "promotion_type"))%>");
它抛出了一个错误:当你没有预料到它时,你有一个 nil 对象! 您可能期望有一个 Array 的实例。 评估 nil.each 时发生错误
#index.erb
<div id="promo_types">
<%= render :partial => "promotion_type" %>
</div>
#_promotion_type
<% @promotion_types.each do |@promotion_type| %>
<table>
<tr>
<td width="200"><%=h @promotion_type.name %></td>
<td width="20"><a class="Edit<%=h @promotion_type.id %>" href="#"><img src="/images/pencil.png" alt="Edit" title="Edit" /></a></td>
<td width="20"><%= link_to(image_tag('/images/bin.png', :alt => 'Remove', :title => "Remove"), @promotion_type, :class => "deleteCategory") %></td>
</tr>
</table>
<div id="popupEdit<%=h @promotion_type.id %>" class="popupEdit">
<a class="popupClose<%=h @promotion_type.id %>" id="popupClose">x</a>
<%= render :file => 'promotion_types/edit' %></div>
<% end %>
当我尝试使用 ajax 追加行时,它不喜欢部分的第一行代码。
我唯一的另一个问题是关于我的 update.js.erb。我工作正常。如果我单击编辑并更新它会更新更改,但在刷新页面之前我看不到它们。我不想 .append 部分,因为这会将编辑添加到列表的底部。我是否使用 .html 通过 AJAX 刷新我的部分内容?
编辑:
#full create.js.erb
$("#promo_types").append(<% escape_javascript(render(:partial => "promotion_type"))%>");
$("new_promotion_type")[0].reset();
$(".addtoggle01").toggle();
$(".addtoggle01").before('<div id="notice">Your promotion type has been successfully submitted.</div>');
它重置我的表单,隐藏表单并显示通知,但它没有附加任何内容,也没有抛出任何错误。
【问题讨论】:
标签: jquery ruby-on-rails ajax