Redirector allows you to easily generate a redirect action for a specific resource.
class Product
belongs_to :store
delegate :parameterize, to: :name
redirect_with base: -> x { x.store.affiliate_uri }, path: :url, epi: :parameterize
end
Define redirect routes:
Foobar::Application.routes.draw do
redirect_for :products
end
Generate the redirect link:
<%= link_for 'Go to product', redirect_product_path(@product) %>
Or use the view helper:
<%= redirect_link_to 'Go to product', @product %>
which generates following html:
<a href="/products/to_param/redirect" data-external="true" rel="nofollow">Go to product</a>
Add tracking url:
Redirect to the page through a tracking url. The url parameter is unescaped.
config/redirector.rb
Redirector.setup do |config|
config.tracking_url = "http://example.com/track/{url}"
end