0)%> 它不起作用,因为它不是链接或跨度元素,而是输出 html 标签 L-6ren">
gpt4 book ai didi

ruby-on-rails - 三元运算符和html

转载 作者:数据小太阳 更新时间:2023-10-29 08:28:10 33 4
gpt4 key购买 nike

我正在尝试在三元运算符的 View 中输出一些 html:

<%params[:type]=="0" ? "<span class=\"active\">Links</span>" : link_to("Links",:type=>0)%>

它不起作用,因为它不是链接或跨度元素,而是输出 html 标签 <span class="active">Links</span>我是 ruby​​/rails 的新手,尝试移动 <%= %> 标签,但找不到方法。

最佳答案

尝试使用 raw method :

<%= params[:type] == "0" ? raw("<span class=\"active\">Links</span>") : link_to("Links",:type=>0) %>

html_safe :

<%= params[:type] == "0" ? "<span class=\"active\">Links</span>".html_safe : link_to("Links",:type => 0) %>

关于ruby-on-rails - 三元运算符和html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13401720/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com