2012-05-08 78 views
1

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

我的视图代码:

<%= 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-<%[email protected]_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 

回答

4

你是在你的erb文件中使用coffeescript。无论是其重命名为destroy.js.coffee,或更改如下:

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

$("#volunteer_posting-<%[email protected]_event.id %>").fadeOut(function(){ 
    $(this).remove(); 
}); 
+0

我意识到这是咖啡,但由于某种原因,我认为这是JS文件中互换。我的错误,并感谢迪伦。这是我的第一个rails应用程序。 – jplumey