Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing ruby code in a haml string

Just adopted an application using HAML and trying to write on top of it (never used HAML before). Been looking at different resources like haml.info and this S.O post. However, the S.O. post is actually incorrect in a couple ways. I'm trying to render your basic bootstrap flash messages.

So far I have this structure:

- flash.each do |key, value|
  .alert.alert-dismissable{ :class => 'alert-#{key}' }
    %button.close{ :type => 'button', :data => { :dismiss => 'alert' }, :aria => { :hidden => 'true' } }
      %i.icon-remove-sign
    = value

Yea that's right, I have to go back to Bootstrap 2 on this one. Seems like everything works, except this line here:

.alert.alert-dismissable{ :class => 'alert-#{key}' }

This gets rendered to HTML as

<div class="alert alert-#{key} alert-dismissable">

Obviously, I want #{key} to be replaced with the key coming from the controller (success, danger, notice, etc) Would love to know what is wrong here. Thanks for taking a look at it.

like image 391
Justin Avatar asked Apr 02 '26 13:04

Justin


1 Answers

"alert-#{key}"

In ruby you can't use #{} to escape single quoted strings.

like image 97
j-dexx Avatar answered Apr 04 '26 07:04

j-dexx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!