我正在使用 resque gem和 bioruby 进行后台处理,处理功能正常。但是我收到了“遗失模板”错误。似乎该操作正在尝试加载模板。
Missing template cosmics/start_batch, application/start_batch with {:locale=>[:en],
:formats=>[:html], :handlers=>[:erb, :builder, :coffee]}.
我不想加载模板:后台进程从外部源检索数据,然后更新数据库表(这一切都正常)。
代码由按钮触发:
<%= link_to 'Process', start_batch_path, :class =>"btn btn-primary" %>
配置/ routes.rb中
match '/cosmics/start_batch', :to => 'cosmics#start_batch', :as => 'start_batch'
resources :batches do
resources :batch_details
end
cosmics_controller.rb
def start_batch
@batch = Batch.create!(:status => 'created',:status_timestamp => Time.now)
@cosmics = Cosmic.find(:all, :conditions => {:selected => true}).each do |cosmic|
@batch_detail = BatchDetail.create!(:batch_id => @batch.id, :cosmic_mut_id => cosmic.cosmic_mut_id)
@batch_detail.save
cosmic.selected = false
cosmic.save
end
Resque.enqueue(UcscQuery,@batch.id)
end
workers / ucsc_query.rb(Reqsue worker class)
class UcscQuery
require 'bio-ucsc'
include Bio
@queue = :ucsc_queue
def self.perform(batch_id)
Ucsc::Hg19.connect
@batch_detail = BatchDetail.find(:all, :conditions => {:batch_id => batch_id}).each do |batch_detail|
ucsc_cosmic = Ucsc::Hg19::Cosmic.find_by_name(batch_detail.cosmic_mut_id)
if ucsc_cosmic
batch_detail.bin = ucsc_cosmic.bin
batch_detail.chrom = ucsc_cosmic.chrom
batch_detail.chrom_start = ucsc_cosmic.chromStart
batch_detail.chrom_end = ucsc_cosmic.chromEnd
batch_detail.status = 'processed'
batch_detail.save
end
end
Batch.update(batch_id, :status => 'located')
end
end
如何防止Rails尝试加载cosmics / start_batch模板?任何重构技巧也将受到赞赏。
答案 0 :(得分:2)
如果控制器方法中没有render
或redirect
指令,Rails会查找具有相同方法名称的视图。要更改此行为,请在render :nothing => true
方法的末尾添加start_batch
。
这样,当用户点击Process
链接时,它将呈现一个空白页面。那肯定不是你想要的。您可以在:remote => true
中使用link_to
选项,以便用户留在当前页面上:
<%= link_to 'Process', start_batch_path, {:remote => true}, {id: 'process_btn', :class => "btn btn-primary"} %>
最后,使用javascript向用户显示他点击按钮时发生的“某事”。例如:
$('#process_btn').on('click', function() { alert('Batch process started'); };
答案 1 :(得分:1)
您只需输入:
即可render :nothing => true
在你的行动中。我还建议你改变你的远程链接
<%= link_to 'Process', start_batch_path, :class =>"btn btn-primary", :remote => true %>
否则点击后会看到空白页面。