NullVoxPopuli
NullVoxPopuli

Reputation: 65183

Dynamically adding a class to a div in a .erb with Ruby on Rails

I have this div

<div class='notice'>

And I want the result to be

<div class="notice error">

And Is there a way to add a class in my erb? I tried

<div class="notice #{new_class}">

But that doesn't escape into ruby code when it renders...

and ideas?

Upvotes: 10

Views: 20195

Answers (2)

Chowlett
Chowlett

Reputation: 46687

I believe

<div class="notice <%= h new_class -%>">

works, albeit a little ugly. You could also do the prettier, but more longwinded

<%= content_tag(:div, "Your content here", :class => "notice #{new_class}") %>

Upvotes: 15

Ju Nogueira
Ju Nogueira

Reputation: 8461

It's with <%= %>

<div class="notice <%= new_class %>">

Upvotes: 32

Related Questions