Rails 4 - 如何编写辅助方法
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 指令的辅助方法?
您的函数将 return 一个字符串,您可能需要 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
我正在尝试弄清楚如何在我的 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 指令的辅助方法?
您的函数将 return 一个字符串,您可能需要 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