Mel
Mel

Reputation: 2687

Rails 4 - how to write a helper method

I'm trying to figure out how to write a helper method in my rails 4 app.

My attempt is below:

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

When I save this and try it, it prints out the css instructions e.g.

<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'> 

How do I write a helper method that uses css to make the output on the page instead of printing the css instructions?

Upvotes: 0

Views: 347

Answers (2)

Arun Kumar M
Arun Kumar M

Reputation: 848

Try this:

def helper_html_safe(raw)
  raw.to_s.html_safe
end

Upvotes: 1

孙悟空
孙悟空

Reputation: 1285

Your function will return a String, you may need raw, html_safe or h to unescape html like this:

In your views:

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

or

items_for_profile_menu(profile).html_safe

or

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

Upvotes: 1

Related Questions