Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby on Rails | 特定の条件に一致する場合にスタイルを指定する

概要

特定の条件に一致する場合にスタイルを指定する

内容

特定の条件に一致する場合にスタイルを指定します。

サンプル

helper

module UserHelper
  def deleted_class(deleted_at)
    "deleted" if deleted_at
  end
end

view

- provide(:title, "ユーザー一覧")
%hr/ 
%table 
  %tr 
    %th 
    %th id
    %th name
    %th login
    %th email
    %th created_at
    %th updated_at
  -@users.each do |u|
    %tr 
    %tr{:class => [cycle(:odd, :even), deleted_class(u.deleted_at)]}
      %td 
        = link_to_unless u.deleted_at, "編集", :controller => 'user', :action => 'edit', id: u.id
        = link_to_unless u.deleted_at, "削除", :controller => 'user', :action => 'delete', id: u.id
      = content_tag :td, u.id
      = content_tag :td, u.name
      = content_tag :td, u.login
      = content_tag :td, u.email
      = content_tag :td, show_dat(u.created_at)
      = content_tag :td, show_at(u.updated_at)

css

.deleted {
  color: red;
}
.deleted:hover {
  color: red;
}

画面