开发者

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

开发者 https://www.devze.com 2023-01-03 13:19 出处:网络
I have this div <div class=\'notice\'> And I want the result to be <div class=\"notice error\">

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 开发者_如何学JAVAescape into ruby code when it renders...

and ideas?


It's with <%= %>

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


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}") %>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号