无法使用acts_as_xlsx gem显示列标题

时间:2013-12-05 17:42:02

标签: ruby-on-rails export-to-excel

我正在使用acts_as_xlsx gem在rails中生成excel报告。我按照示例创建了列标题,方法是将它们添加到我的en.yml文件中,包括:i18n =>在我的模型中为true,但行为空白。所有其他数据都正确显示。

控制器:

def index
  @claims = Claim.all

  respond_to do | format |
    format.html # index.html.erb
    format.json { render :json => @claims }
    format.xlsx {

    xlsx_package = Claim.to_xlsx :name => "Claims", 
                               :header_style => {:bg_color => "377", 
                               :fg_color => "FF",  
                               :sz => 16, 
                               :alignment => { :horizontal => :center }}
    begin
      temp = Tempfile.new("claims.xlsx")
      xlsx_package.serialize temp.path
      send_data xlsx_package.to_stream.read, :filename => 'claims.xlsx', :type=> "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    ensure
      temp.close
      temp.unlink
    end
  }  
  end
end

型号:

acts_as_xlsx :columns => [:id, :business_unit_id, :created_at, :updated_at, :claim_number], :i18n => true

En.yml:

en:
  activerecord:
    attributes:
      claims: Claims
      claim:
        id: "Claim Id"
        business_unit_id: "Business Unit"
        created_at: "Created at"
        updated_at: "Updated at"
        claim_number: "Claim Number"

1 个答案:

答案 0 :(得分:0)

它无法正常工作的原因是您使用复数形式的声明的en.yml文件,您应该使用声明。

en:
  activerecord:
    claim:
      id: "Claim Id "
      business_unit_id: "Business Unit "
      created_at: "Created at "
      updated_at: "Updated at "
      claim_number: "Claim Number "

我认为应该用于i8n的开关实际上是“activerecord.attributes”,基于最新的文档。

:i18n => "activerecord.attributes"

另一方面,我认为您需要在标题后添加一个空格,否则您在表单错误中显示的标题将无法正确间隔。