0

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.

Community
  • 1
  • 1
edev.io
  • 560
  • 5
  • 21

1 Answers1

1

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.

deefour
  • 34,974
  • 7
  • 97
  • 90
  • brilliant thanks. I knew it was simple :-). Any tips on keeping it tidy when there are multiple `can? :edit` conditions? e.g. sites, campaigns etc? or do I just have to bite the bullet on this one? Thanks again – edev.io Dec 11 '12 at 19:42
  • If a single conditional like the one above has multiple conditions and is reused frequently, you might consider putting it into a helper method, however the sort of thing you're doing typically only happens a lot in one place: the navigation partial. If that's the case, I say just "bite the bullet". I have more detailed responses I could provide to this, but it's a bit out of the scope of this question. – deefour Dec 11 '12 at 19:52
  • Yeah not to worry. that is more than enough help. Thanks – edev.io Dec 11 '12 at 20:00