1

我正在使用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" %>

配置/路由.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(请求工人类)

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 模板?任何重构技巧也将不胜感激。

4

2 回答 2

2

如果控制器方法中没有rendernorredirect指令,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'); };
于 2013-06-10T11:29:36.363 回答
1

您可以简单地键入:

render :nothing => true

在你的行动中。我还建议将您的链接更改为远程

<%= link_to 'Process', start_batch_path, :class =>"btn btn-primary", :remote => true %>

否则点击后你会看到空白页。

于 2013-06-10T11:30:02.760 回答