Ruby on rails 将样式添加到从Rails控制器输出的format.html

Ruby on rails 将样式添加到从Rails控制器输出的format.html,ruby-on-rails,Ruby On Rails,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 maintena

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
报告控制器

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

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

= render @report.method, :report => @report
我想在我的视图中用以下标记格式化html表格
%table.table.datatable#datatable

这:


不起作用…

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

作为一个简单的示例,您可以为视图创建一个与
方法
控制器方法完全相同的帮助器,如:

# 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
}

将规则集添加到样式表目标
#datatable
?查看我如何在视图中显示我的报告。我只是称之为控制器显示。因为有多个报表,所以应该动态创建它们,但我需要将datatable css标记添加到表中,这样我就可以启用排序..
format.html
而无需任何参数,只需调用
render“show”
。像平常一样添加样式。我需要传入要显示的报表。那么,我如何根据上面的代码添加CSS呢?谢谢
table.maintenance_history {
  // style accordingly
}

table.idle_time_report {
  // style accordingly
}