Rails链接到远程放置方法不刷新抛出Nihlclass



我有一个Rails 3.2.14应用程序,其中我有调用,调用有单元,单元有状态,以及模型中的调用时间,如in_service_time。

我正在尝试编写一个名为in_service的控制器操作,它将单元的状态更新为"in service",并将时间戳call.in_service_time更新为time.zone.now,并通过put remote(Ajax)完成所有这些操作。

到目前为止,我所写的似乎是通过更新单元状态和在call.in_service_time上加时间戳来工作的。但使用:remote=>true并不能像我在in_service操作中指定的那样呈现部分。我必须等待屏幕通过index.html.erb中的getScript调用刷新,以便更新调用状态。如果我使用:remote=>false,它会立即重新加载页面而不会出现问题。如果我使用:remote=>true,我还会在NihlClass的development.log中抛出一个异常。

以下是有问题的代码的摘录,完整的代码可以在这里找到,以使阅读更容易:完整的代码

index.html.erb

<div id="active">
<%= render "assigned_calls" %>
</div>
<div id="inactive">
<%= render "unassigned_calls" %>
</div>

<script>
$(function() {
setInterval(function(){
$.getScript("/calls").fail(function(jqxhr, settings, exception) {
window.location = "/users/sign_in?duplicate_session=true";
});
}, 10000);
});
</script>

index.js.erb

$("#active").html("<%= escape_javascript render("assigned_calls") %>");
$("#inactive").html("<%= escape_javascript render("unassigned_calls") %>");

routes.rb摘录

resources :calls do
member do
post 'close'
post 'cancel'
post 'note'
get 'opencall'
get  'new_return'
get 'duplicate_call'
get 'edit_times'
put 'update_billing'
post 'dispatch_call'
put 'en_route'
put 'on_scene'
put 'to_hospital'
put 'at_hospital'
put 'in_service'
end

calls_controller.rb摘录

def in_service
@call = Call.find(params[:id])
@unit = @call.units.first
@call.update_attributes(in_service_time: Time.zone.now)
@call.save
@unit.status = Status.find_by_unit_status("In Service")
@unit.save
respond_to do |format|
format.html { redirect_to calls_url }
format.js { render "index" }
end
end

_assigned_calls.html.erb

<div class="page-header">
<span class="badge badge-important"><%= @unassigned.count %></span> <strong>Unassigned calls</strong>
<span class="badge badge-info"><%= @assigned.count %></span> <strong>Active calls</strong>
<span class="badge badge-warning"><%= @scheduled.count %></span> <strong>Scheduled calls</strong>
<%= render "search" %>
<h2>Active Calls</h2>

</div>
<% @assigned.each do |call| %>
<div class="widget">
<div class="widget-header">
<div class="pull-right">
<%= link_to 'View', call, :class => 'btn btn-close'%>
<% if dispatch? %>
<%= link_to 'Edit', edit_call_path(call), :class => 'btn btn-close'%>
<%= link_to 'Close', close_call_path(call), confirm: 'Are you sure you want to close the call?', :method => :post, :class => 'btn btn-danger' %>
<%= link_to 'Cancel', cancel_call_path(call), confirm: 'Are you sure you want to cancel the call?', :method => :post, :class => 'btn btn-warning' %>
<% end %>
</div>
<i class="icon-phone"></i>
<h3><%= link_to call.incident_number, call %> <span><%= status(call) %></span></h3> 
<% if call.traffic_type == "Emergency" %>
<span class="badge badge-important"><%= call.traffic_type %></span>
<% else %>
<span class="badge badge-info"><%= call.traffic_type %></span>
<% end %>
</div>
<div class="widget-content">
<div class="row">
<div class="span3">
<h4>Patient Name: <small><%= call.patient_name %></small></h4>
<div class="large-display">
<% if call.call_status == "open" %>
<div class="large <%= elapsed_overdue(call.elapsed_time) %>"><%= TimeFormatter.format_time(call.elapsed_time)%></div>
<div class="small">Elapsed Time</div>
<% else %>
<% if call.closed_at? %>
<div class="large <%= elapsed_overdue(call.run_time) %>"><%= TimeFormatter.format_time(call.run_time)%></div>
<div class="small">Run Time</div>
<% end %>
<% end %>
</div>
</div>
<div class="span2">
<address>
<strong><%= transferred_from(call) %></strong><br>
<%= transferred_from_address(call) %>
</address>
</div>
<div class="span1"><i class="icon-arrow-right dim"></i></div>
<div class="span2">
<address>
<strong><%= transferred_to(call) %></strong><br>
<%= transferred_to_address(call) %>
</address>
</div>
<div class="span3">
<% if call.service_level.level_of_service == "WC" %>
<div class="left-icon dim"><i class="icon-user"></i></div>
<% else %>
<div class="left-icon dim"><i class="icon-ambulance"></i></div>
<% end %>
Assigned to <strong>Unit <%= call.units.map(&:unit_name).join(", ") %></strong><br />
<% call.units.each do |unit| %>
<div class="<%= set_status(unit.status) %>"><%= unit_status(unit) %></div>
<% end %>
</div>
</div>
<hr />
<div class="row">
<div class="span2">
<div class="large-display">
<div class="medium <%= transfer_due(call.transfer_date) %>"><%= call.transfer_date.strftime("%m/%d/%y %H:%M") %></div>
<div class="small">Transfer Date</div>
</div>
</div>
<div class="span1">
<div class="large-display">
<div class="small"><%= call.nature.try(:determinant) %></div>
<div class="small">Nature</div>
</div>
</div>

<div class="span5">
<% if call.unit_ids.present? %>
<div class="large-display">
<div class="progress progress-striped"><%= progress_bar(call) %></div>
<div class="small"><%= call.units.first.status.unit_status %></div>
<div><%= link_to 'En Route', en_route_call_path(call), :class => 'btn btn-warning btn-medium', :method => :put, :remote => true %><%= link_to 'On Scene', on_scene_call_path(call), :class => 'btn btn-primary btn-medium', :method => :put, :remote => true %><%= link_to 'To Hospital', to_hospital_call_path(call), :class => 'btn btn-warning btn-medium', :method => :put, :remote => true %><%= link_to 'At Hospital', at_hospital_call_path(call), :class => 'btn btn-danger btn-medium', :method => :put, :remote => true %><%= link_to 'In Service', in_service_call_path(call), :class => 'btn btn-success btn-medium', :method => :put, :remote => true %></div>
<div>ER: <%= call.en_route_time.try(:strftime, "%m/%d/%y-%k:%M") %> OS: <%= call.on_scene_time.try(:strftime, "%m/%d/%y-%k:%M") %> TO: <%= call.to_hospital_time.try(:strftime, "%m/%d/%y-%k:%M") %> AT: <%= call.at_hospital_time.try(:strftime, "%m/%d/%y-%k:%M") %>  IS: <%= call.in_service_time.try(:strftime, "%m/%d/%y-%k:%M") %></div>
</div>
<% end %>
</div>

<div class="span3">
<div class="left-icon dim"><i class="icon-user"></i></div>
<%= render partial: "medics_for_call", locals: {call: call} %>
</div>
</div>
</div>
</div>
<% end %>

development.log摘录

NoMethodError - undefined method `count' for nil:NilClass:
app/views/calls/_assigned_calls.html.erb:2:in `_app_views_calls__assigned_calls_html_erb___2549181816739942207_70125964034780'

您永远不会在in_service操作中设置@unassigned。因此,向其发送count会引发异常。

从其他操作呈现操作的视图(render "index")不会执行该操作的控制器代码。

编辑:如何使其干燥:

class CallsController < ApplicationController
...
def index
setup_scheduling_variables
@units = Unit.order("unit_name")
@overdue = @assigned.select{|call| call.elapsed_time > 3600}
@inservice = Unit.in_service
end

def in_service
...
respond_to do |format|
format.html { redirect_to calls_url }
format.js do 
setup_scheduling_variables
render "index" 
end
end
end
private
def setup_scheduling_variables
@assigned = params[:search].present? ? Call.search(params[:search]) : Call.assigned_calls.until_end_of_day.order("transfer_date ASC")
@unassigned = Call.unassigned_calls.until_end_of_day
@scheduled = Call.scheduled_calls
end

相关内容

  • 没有找到相关文章

最新更新