css
  • ruby-on-rails
  • helper
  • view-helpers
  • 2016-08-01 59 views 0 likes 
    0

    我想弄清楚如何在我的Rails 4應用程序中編寫一個輔助方法。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指令?

    回答

    1

    你的函數會返回一個字符串,您可能需要rawhtml_safeh反轉義HTML這樣的:

    在您的觀點:

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

    items_for_profile_menu(profile).html_safe 
    

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

    試試這個:

    def helper_html_safe(raw) 
        raw.to_s.html_safe 
    end 
    
    相關問題