在 Rails 上使用 Ruby 在 Slim 中有条件地创建隐藏属性

Conditionally creating a hidden attribute in Slim with Ruby on Rails

如果条件为真,我正在尝试让 Slim 生成以下内容HTML:

<div id="start_button" hidden="hidden">

我尝试过各种方法,比如显而易见的方法:

#start_button #{('hidden="hidden"' if CONDITION?)}
     = link_to 'Get Started', ...etc...

但这会产生:

<div id="start_button">hidden="hidden"
= link_to 'Get Started',..etc...

我知道在设置诸如 class 等属性时该怎么做,但是因为它必须是“隐藏”的全部或全部,这给我带来了问题。

今晚我遇到了很多麻烦,所以非常感谢您的帮助!

这在文档中有说明:

Ruby attributes

Write the ruby code directly after the =. If the code contains spaces you have to wrap the code into parentheses (...). You can also directly write hashes {...} and arrays [...].

因此,() 中的任何内容都被评估为 Ruby 代码,如果该语句被评估为 false,它会以某种方式“跳过”,否则它会执行 true 分支中所述的操作。

#start_button hidden=('hidden' if true)
<div hidden="hidden" id="start_button"></div>

#start_button hidden=('hidden' if false)
<div id="start_button"></div>