[英]Rails 4 - how to write a helper method
我试图弄清楚如何在Rails 4应用程序中编写一个辅助方法。
我的尝试如下:
module ProfilesHelper
def items_for_profile_menu(profile)
if current_user = @profile.user_id
"<li class='col-xs-4 col-sm-2 nopadding menuitem' style='background:#006F7F'>
<a href='index.html' class='hvr-sweep-to-bottom'>
# link_to dashboard_path(@profile.dashboard)
<span>Dashboard</span>
</a>
</li>
<li class='col-xs-4 col-sm-2 nopadding menuitem' style='background:#39AFBF'>
<a href='#resume' class='hvr-sweep-to-bottom'>
<!-- <i class='flaticon-graduation61'></i> -->
<br><br>
<span>Timeline</span></a>
</li>"
else
"<li class='col-xs-6 col-sm-3 nopadding menuitem blue'>
<a href='resume.html' class='hvr-sweep-to-bottom'>
<i class='flaticon-graduation61'>
</i><span>Researh History</span></a>
</li>
<li class='col-xs-6 col-sm-3 nopadding menuitem cyan'>
<a href='#portfolio' class='hvr-sweep-to-bottom'><i class='flaticon-book-bag2'></i><span>Projects & Programs</span></a>
</li>"
end
end
end
当我保存并尝试时,它会打印出CSS指令,例如
<li class='col-xs-4 col-sm-2 nopadding menuitem' style='background:#006F7F'> <a href='index.html' class='hvr-sweep-to-bottom'> # link_to dashboard_path(@profile.dashboard) <span>Dashboard</span> </a> </li> <li class='col-xs-4 col-sm-2 nopadding menuitem' style='background:#39AFBF'> <a href='#resume' class='hvr-sweep-to-bottom'> <!-- <i class='flaticon-graduation61'></i> --> <br><br> <span>Timeline</span></a> </li> <li class='col-xs-4 col-sm-2 nopadding menuitem' style='background:009CB2'> <a href='#portfolio' class='hvr-sweep-to-bottom'>
如何编写一个使用CSS的辅助方法,而不是打印CSS指令在页面上输出?
您的函数将返回一个String,您可能需要raw
, html_safe
或h
来对html进行转义,如下所示:
在您看来:
<%= raw (items_for_profile_menu(profile)) %>
要么
items_for_profile_menu(profile).html_safe
要么
<%=h (items_for_profile_menu(profile)) %>
尝试这个:
def helper_html_safe(raw)
raw.to_s.html_safe
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.