Himmators
Himmators

Reputation: 15006

Extend link_to function?

I wish to add a helper that works exactly like link_to but adds an url-parameter to the link. I tried this:

  def tracked_link_to(url, id, options)
    link_to('url?utm_link=#{id}', options)
  end

And the I tried calling it like this:

  <% tracked_link_to '/', 'logo' , alt: 'Zenconomy', class: 'logotype', itemprop: 'url' do %>
    <%= svg 'logotype-icon', 'icon-logo' %>
  <% end %>

but for some reason it doesn't output anything.

Upvotes: 1

Views: 204

Answers (1)

BroiSatse
BroiSatse

Reputation: 44685

You missed equal sign. Should be:

<%= tracked_link_to '/', 'logo' , alt: 'Zenconomy', class: 'logotype', itemprop: 'url' do %>

Apart of that your implementation of tracked_link_to will very likely yield a different result than you are expecting. And i got to worry you, it will not be that easy to implement. Firstly, link_to works with strings, hashes or objects:

link_to('object/1')
link_to(object)
link_to(action: :show)

Secondly, string can already include query params:

tracked_linked_to('object/1?preview=true')

in which case you need to use & instead of ?. Also, what would you do in case if string url already include utm_param?

Upvotes: 3

Related Questions