【问题标题】:Rails, jQuery, Ajax create and update.js.erbRails、jQuery、Ajax 创建和更新.js.erb
【发布时间】: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


    【解决方案1】:

    在您的_promotion_type 部分中,您正在循环通过@promotion_types,听起来这个实例变量没有在create 操作中设置。

    从名字上看,partial 应该只显示一种促销类型,而不是很多。所以将循环移到部分之外。我建议使用 :collection:object 选项将模型传递给部分。

    # index.html.erb
    <div id="promo_types">
      <%= render :partial => "promotion_type", :collection => @promotion_types %>
    </div>
    
    # _promotion_type.html.erb
    <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>
    
    # create.js.erb
    $("#promo_types").append("<%= escape_javascript(render(:partial => "promotion_type", :object => @promotion_type))%>");
    

    至于你的第二个问题,是的。您可以为每条记录分配一个特定的 ID,并使用 .html 方法替换它。

    # update.js.erb
    $("#promo_type_<%= @promotion_type.id %>").html("<%= escape_javascript(render(:partial => "promotion_type", :object => @promotion_type))%>");
    

    【讨论】:

    • 我把循环扔到了部分之外,我再也没有收到那个错误了。谢谢。但是我的 .append 和 .html 根本没有效果。我会进行编辑,它会显示代码。
    • 确保在escape_javascript之前添加&lt;%=中的等号。
    • 我认为那是存在的。该代码在另一台计算机上,所以我正在输入它并且可能忽略了它。明天我回去工作后再去看看。
    • 如果这不是问题,请尝试使用 FireBug 之类的方法,以确保没有任何 javascript 错误。 getfirebug.com
    • 我一直使用萤火虫。刚回去工作,我错过了=所以现在可以了!非常感谢大家的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多