Rails 4 - 如何编写辅助方法

时间:2016-08-01 06:17:30

标签: css ruby-on-rails helper view-helpers

我试图弄清楚如何在我的rails 4 app中编写辅助方法。

我的尝试如下:

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指令的辅助方法?

2 个答案:

答案 0 :(得分:1)

您的函数将返回一个字符串,您可能需要rawhtml_safeh这样的unescape html:

在您的观点中:

<%= raw (items_for_profile_menu(profile)) %>

items_for_profile_menu(profile).html_safe

<%=h (items_for_profile_menu(profile)) %>

答案 1 :(得分:1)

试试这个:

def helper_html_safe(raw)
  raw.to_s.html_safe
end