edev.io
edev.io

Reputation: 560

List Item Wrapping Link_to in Conjunction with Can

I'm trying to do something very basic but am getting tripped up.

I am creating anchor tags using link_to, but I want these tags to be wrapped in li tags. I followed the steps on a previous post Rails 3: Link_to list item? but this wraps my LIs in As. I've tried this.

<%= link_to "<li>Site Specific Articles</li>".html_safe, site_specific_articles_path if can? :edit, SiteSpecificArticle %>

but it produces

<a href="/site_specific_articles"><li>Site Specific Articles</li></a>

when I want

<li><a href="/site_specific_articles">Site Specific Articles</a></li>

Any ideas are more than welcome on this.

Thank You.

Upvotes: 0

Views: 656

Answers (1)

deefour
deefour

Reputation: 35370

You have to keep the <li> and </li> out of the link_to, meaning the conditional must wrap everything (it needs to be taken out of the link_to as well)

<% if can? :edit, SiteSpecificArticle %>
  <li>
    <%= link_to "Site Specific Articles", site_specific_articles_path %>
  </li>
<% end %>

IMO, this is more readable anyway.

Upvotes: 1

Related Questions