Topic: How to add a <span> </span> to link_to ????

Can someone please advise where/how to put a <span> in link_to ?
I have the following code and want to add background images with CSS that require the link --- link_to page.title ---- to have a <span>

<% @tabs.each do |page| -%>
<%= link_to page.title, view_page_path(page.name) >
< end -%>

Many thanks!

Re: How to add a <span> </span> to link_to ????

Depending on how widespread this requirement is through your application, I can think of a couple of ways to do it...

1. If it's just in a few places, then write a helper in application_helper.rb

def liams_link_to(text, path)
  out = "<span class='liams_link'>"
  out += link_to text, path
  out += "</span>"
  out
end

and call it like this: liams_link_to page.title, view_page_path(page.name)

2. alternatively if you're using this (almost) everywhere in your app, I would alias the ActionView link_to method with:

alias_method_chain  :link_to, :span_tags

then write your own link_to like this

def link_to(text, path)
  out = "<span class='liams_link'>"
  out += link_to_without_span_tags text, path
  out += "</span>"
  out
end

calling it with just link_to. And if somewhere you need the standard link_to, it's just link_to_without_span_tags

hope this helps

Les

Re: How to add a <span> </span> to link_to ????

Why bother with all that, when you can just use the HTML way of doing it from the get go...

<% @tabs.each do |page| -%>
<span class="page_link"><%= link_to page.title, view_page_path(page.name) ></span>
< end -%>

Then put whatever styling you want for that class in your stylesheet of course...

Re: How to add a <span> </span> to link_to ????

why bother with all that?

'cos it's DRY!

Re: How to add a <span> </span> to link_to ????

LOL

Good point. smile