Search code examples
rubyruby-on-rails-3link-to

Link_to with a custom method and adding html options


Could anyone advise how to set out the following link_to please, i thought that any html option ( and that includes a class doesnt it?) should be in {}...

So i have this link_to at the moment

<%= link_to(@portfolio.previous_post) if @portfolio.previous_post %>

I would like to add a class of 'prev' as this has a background image within the css, rather than using text..I am unsure on where to put

:class => 'prev' or class: 'prev'

depending on preference of course

This doesn't work

<%= link_to(@portfolio.previous_post, { class: 'prev'}) if @portfolio.previous_post %>

Any help appreciated


Solution

  • Something like this should work:

    `    <% if @portfolio.previous_post %>
             <%= link_to "link_name",@portfolio.previous_post, class: "prev"%>
         <% end %>`
    

    This should work as well:

    `    <%= link_to_if(@portfolio.previous_post, "link_name", @portfolio.previous_post, 
            class: "prev") %>`
    

    Once you provide a name for the url in addition to the url, everything seems to work.