浏览器没有响应destroy.js.erb

时间:2012-05-08 15:23:43

标签: jquery ruby-on-rails activerecord

我有一个管理志愿者和活动的Rails 3.2.3应用程序。在我的一个观点中,我正在建立一个摧毁志愿者活动的链接。我正在使用data-remote = true。控制器正确运行,Rails说它正在渲染我的destroy.js.erb,但我的文件中的脚本没有被调用。我的脚本正在尝试删除DIV元素并更新范围的内容。以下是一些细节:

我的观看代码:

<%= link_to '', event, method: :delete, confirm: "Are you sure you want to delete this  volunteer posting?", title: event.name, class: "icon-remove", remote: true%> </li>

我的控制器代码:     ...

respond_to  :html, :js

def destroy
    @volunteer_event = VolunteerEvent.find(params[:id])
    @volunteer_event.destroy

    respond_with(@volunteer_event) 

end

我的destroy.js.erb

$("#hours_worked_total").html("<%= @volunteer_event.worker.account.total_hours_worked %>")

$("#volunteer_posting-<%=@volunteer_event.id %>")
  .fadeOut ->
      $(this).remove()

这是在视图中构建的结果HTML:

<ol class="events">
<div id="volunteer_posting-48">
<li><strong>9808</strong> worked on August  5, 2012 by <i>Louie Ferry</i> for a total of 5 hours. 
<a href="/volunteer_events/48" class="icon-remove" data-confirm="Are you sure you want to delete this volunteer posting?" data-method="delete" data-remote="true" rel="nofollow" title="9808"></a> </li>
</div>
</ol>

这是服务器日志:

Started DELETE "/volunteer_events/48" for 127.0.0.1 at 2012-05-08 11:17:50 -0400
...
Rendered volunteer_events/destroy.js.erb (2.7ms)

这是浏览器请求和回复:

Response:
$("#hours_worked_total").html("0")


$("#volunteer_posting-48")
   .fadeOut ->
   $(this).remove()

Content-Type:text/javascript; charset=utf-8

1 个答案:

答案 0 :(得分:4)

您在erb文件中使用coffeescript。将其重命名为destroy.js.coffee,或按如下所示进行更改:

$("#hours_worked_total").html("<%= @volunteer_event.worker.account.total_hours_worked %>");

$("#volunteer_posting-<%=@volunteer_event.id %>").fadeOut(function(){
  $(this).remove();
});