Search code examples
ruby-on-railsrubymiddleman

Add arbitrary string to URLs in Rails/Middleman app


I'm using middleman to generate a static webpage. I need to add a consistent but understandable string to all urls so I can understand how users navigate on the page. Now i do it like this

<% link_to '/'+?button=navigation , class: 'logotype', itemprop: 'url' do %> 
  ...
<% end %>

I would prefer not having to manually add all the parameters but rather just use something that's already there, like a scope or something. I was thinking about using the name of the template file for example. The url is not unique enough.

Any suggestions?


Solution

  • The standard way of doing this would be to write a helper method that encapsulates your functionality:

     <%= link_to_as_nav('/', class: 'logotype', ...) do %>
       ...
     <% end %>
    

    Then write a helper method:

     def link_to_as_nav(url, options)
        link_to(url + '?button=navigation', options)
     end
    

    This is the naïve approach and won't account for a url argument that already has parameters added, but that's something you can work to fix.