-1

reports.rb

def method
  if self.name == "Maintenance History"
    'maintenance_history'
  elsif self.name == "Outstanding Maintenance"
    'outstanding_maintenance'
  elsif self.name == "Idle Time Report"
   'idle_time_report'
  end
end

def maintenance_history
  maintenance_history.where(....)
end

def outstanding_maintenance
  outstanding_maintenance.where(....)
end

def idle_time_report
  idle_time_report.where(....)
end

reports_controller

  def show
    @report = Report.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb

show.html.haml

= render @report.method, :report => @report

I would like to format the html table in my view with the following tag %table.table.datatable#datatable

This:

%table.table.datatable#datatable
  = render @report.method, :report => @report

does not work...

4

1 回答 1

0

如果我正确理解您的问题,那就是不同的表格需要不同的样式,具体取决于报告。正如您在控制器中使用报表的名称来确定范围一样,您可以在视图中使用报表的某些属性向 HTML 添加类或其他标识属性。

作为一个简单的例子,你可以为你的视图创建一个助手,就像你的method控制器方法一样,比如:

# some helper for the reports
module ReportsHelper

  # the `method` method from your controller, migrated to a helper
  def report_table_class(report)
    if report.name == "Maintenance History"
      'maintenance_history'
    elsif report.name == "Outstanding Maintenance"
      'outstanding_maintenance'
    elsif report.name == "Idle Time Report"
      'idle_time_report'
    end
  end
end

然后在您的视图中,您可以使用它来划分表格或父元素,您可以将其用作样式选择器的目标:

%table#datatable{:class => ['table', 'datatable', report_table_class(@report)]}

最后在你的 CSS 中:

table.maintenance_history {
  // style accordingly
}

table.idle_time_report {
  // style accordingly
}
于 2013-06-20T16:45:30.797 回答