stuartchaney
stuartchaney

Reputation: 432

Attaching a parameter to each GET Request in Rails

Looking to see if there is an elegant solution for adding a parameter to each GET request based upon a session value in a Rails app. (4.2.6 if needed)

Example:

Love to hear any ideas on this - thanks!

Upvotes: 1

Views: 192

Answers (3)

DevPro
DevPro

Reputation: 511

The routes can be generated as:

random_path(random, test: session[:test]) 

It will redirect to:

example.com/random_path?test=123

where test = session[:test]

Upvotes: 0

JP Silvashy
JP Silvashy

Reputation: 48525

If you want the generated URLs to that path to always have a certain parameter in them when they are generated, you can write your own url_for or link_to helper for that specific route, or override default_url_options and set this parameter if the action or controller name match some condition:

class ApplicationController < ActionController::Base
  def default_url_options(options = {})
    if controller_name == 'my_controller'
      options[:test] = session[:test]
    end

    options
  end
end

Upvotes: 1

hd1
hd1

Reputation: 34657

Would redirect_to be a solution? See the code below:

redirected_url = "https://foo.com?test=#{session[:test]}"

redirect_to redirected_url

Hope it helps....

Upvotes: 0

Related Questions