Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails add parameter to each URL in the application

I would like to add a parameter to each URL in my Rails 2.3.10 application. I played with the default_url_options but I want the parameter to be visible in the URL. Something like:

http://<server>/posts?token=XYZ

I'm building an affilate tracking system and I want people to share the link and be able to track who's link was used later when someone clicks it (to give points to the guy who shared the link). Any hints how I can add a visible parameter in each URL used in the application?

Thanks!

like image 677
Cimm Avatar asked Nov 05 '10 12:11

Cimm


People also ask

How params works in Rails?

With params . Inside your controller action's you can call params to access form & URL query data. What is params , exactly? It's a method that returns an ActionController::Parameters object, in practice it behaves a lot like a hash.

What is get parameter in URL?

GET parameters (also called URL parameters or query strings) are used when a client, such as a browser, requests a particular resource from a web server using the HTTP protocol. These parameters are usually name-value pairs, separated by an equals sign = . They can be used for a variety of things, as explained below.

How do I get the current URL in Ruby?

You should use request. original_url to get the current URL.


2 Answers

Rewrite url_for

module ActionView::Helpers::UrlHelper
  def url_for options
    options.merge! {:token => generate_token}
    super
  end
end

or just add this to your application.rb file

config.default_url_options += {:token => proc{generate_token}}
like image 61
edgerunner Avatar answered Sep 24 '22 15:09

edgerunner


define default_url_options in ApplicationController (or the relevant controller)

def default_url_options
  {subdomain: 'www'}
end
like image 27
Steven Soroka Avatar answered Sep 24 '22 15:09

Steven Soroka