Reputation: 49
I have content_tag with "td", where I must write codition on ruby. I wrote in the following way:
content_tag :td, class: "text-right<%= ' text-danger' if section.sum_control != (section.sum_self_summary + section.sum_sub_summary) %>", style: "width: 150px"
But it didn't work, how decide this problem?
Upvotes: 2
Views: 213
Reputation: 106802
I would add a method to the model to determine its state, because IMO view should not contain complex conditionals that depends model attributes:
# in the Section model
def sums_ok?
sum_control == (sum_self_summary + sum_sub_summary)
end
And a method to the helper to determine the css class:
def css_class_for_section(section)
css = ['text-right']
css << 'text-danger' unless section.sums_ok?
css.join(' ')
end
Than the content_tag
might look like:
content_tag(:td, class: css_class_for_section(section), style: 'width: 150px')
Furthermore I think it makes sense to remove the style tag from the td
and move that into the stylesheets.
Upvotes: 0
Reputation: 1747
May be you should do like this
cls = section.sum_control != (section.sum_self_summary + section.sum_sub_summary) ? 'text-right text-danger' : 'text-right'
content_tag(:td, class: cls, style: 'width: 150px')
Hope this is helpful
Upvotes: 2
Reputation: 83
content_tag :td, class: "text-right#{ ' text-danger' if section.sum_control != (section.sum_self_summary + section.sum_sub_summary) }", style: "width: 150px"
Is your content tag is inside <%= %>, if yes then I think you should use interpolation #{} for if condition
Upvotes: 2