小编Oli*_*ale的帖子

Rails 3:如何在没有模板文件的情况下响应csv?

我有一个有一个to_csv方法的对象,我想将它传递respond_with给我的控制器渲染csv.我的代码看起来像这样:

class Admin::ReportsController < AdminController

  respond_to :csv

  def trips
    respond_with TripReport.new
  end
end
Run Code Online (Sandbox Code Playgroud)

TripReport的实例具有to_csv方法.

当我向该操作发出请求时,我收到以下错误:

ActionView::MissingTemplate (Missing template admin/reports/trips with {:formats=>[:csv], :handlers=>[:erb, :builder, :rjs, :rhtml, :rxml], :locale=>[:en, :en]} in view paths
Run Code Online (Sandbox Code Playgroud)

所以看起来控制器正在寻找要渲染的模板文件.我怎么能绕过这个?

我宁愿csv格式以类似于json的方式响应,所以它调用to_csv对象并只渲染输出,这可能吗?

csv ruby-on-rails

18
推荐指数
2
解决办法
2万
查看次数

Rails 3.1模板处理程序

我有一个ruby gem,poirot,可以在Rails中使用小胡子模板.我的模板处理程序是从ActionView :: Template :: Handler扩展,但是在Rails 3.1中似乎不推荐使用它.

我重新考虑了处理程序以遵守弃用警告.在执行此操作时,我现在无法将本地或视图上下文传递给模板.我似乎无法找到如何使用Rails 3.1.

module Poirot
  class Handler

    attr_reader :template

    def initialize(template)
      @template = template
    end

    def self.call(template, *args)
      self.new(template).call
    end

    def call
      view_path = "#{template.virtual_path}_view"
      abs_view_path = Rails.root.join('app/views', view_path)
      view_class = begin
        view_path.classify.constantize
      rescue NameError => e
        Poirot::View
      end
      "#{view_class}.new(self, '#{template.source.gsub(/'/, "\\\\'")}').render.html_safe"
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

在我上面的处理程序代码中,我传递了模板,它是ActionView :: Template的一个实例.但我不知道如何获取视图上下文,其中应包括本地等

有人能指出我正确的方向吗?

ruby ruby-on-rails actionview mustache

5
推荐指数
1
解决办法
1723
查看次数

标签 统计

ruby-on-rails ×2

actionview ×1

csv ×1

mustache ×1

ruby ×1