【问题标题】:Browser not responding to destroy.js.erb浏览器没有响应 destroy.js.erb
【发布时间】:2012-05-08 15:23:43
【问题描述】:

我有一个管理志愿者和活动的 Rails 3.2.3 应用程序。在我的一个观点中,我正在创建一个链接来破坏志愿者活动。我正在使用数据远程=真。控制器运行正常,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

【问题讨论】:

    标签: jquery ruby-on-rails activerecord


    【解决方案1】:

    您在 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();
    });
    

    【讨论】:

    • 我意识到这是咖啡,但出于某种原因,我认为它可以在 JS 文件中互换。我的错误,感谢迪伦。这是我的第一个 Rails 应用程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多