简体   繁体   中英

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 SO post . However, the SO 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.

"alert-#{key}"

在ruby中,您无法使用#{}来转义单引号字符串。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM