# Passing parameter automatically to links

**URL:** <https://www.ruby-forum.com/t/passing-parameter-automatically-to-links/191226>\
**Category:** Rails\
**Created:** [July 7, 2010, 6:23pm UTC](https://www.ruby-forum.com/t/passing-parameter-automatically-to-links/191226 "2010-07-07T18:23:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sigma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@sigma](https://www.ruby-forum.com/u/sigma)\
**Post date:** [July 7, 2010, 6:23pm UTC](https://www.ruby-forum.com/t/passing-parameter-automatically-to-links/191226/1 "2010-07-07T18:23:06Z")

</div>

Hi there

Is it possible to pass a parameter automatically to every rails  
generated link?

for example:

I have the parameter site\_id which is passed via url  
([www.example.com/:site\_id/:controller/:action/:id](http://www.example.com/:site_id/:controller/:action/:id))

is it possible to automatically pass the :site\_id to all links/paths  
like f.i. “articles\_path”, “article\_path(@article)” or is the only way  
to do this by passing the parameter in addition:  
“articles\_path(:site\_id=\>params[:site\_id])” or  
“article\_path(@article, :site\_id=\>params[:site\_id])”.

Thanks for your answers

Regards  
sigma

---

<div class="post-metadata">

**Author:** ![sigma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@sigma](https://www.ruby-forum.com/u/sigma)\
**Post date:** [July 7, 2010, 7:05pm UTC](https://www.ruby-forum.com/t/passing-parameter-automatically-to-links/191226/2 "2010-07-07T19:05:51Z")

</div>

On 7 July 2010 17:21, sigma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> is it possible to automatically pass the :site\_id to all links/paths  
> like f.i. “articles\_path”, “article\_path(@article)” or is the only way  
> to do this by passing the parameter in addition:  
> “articles\_path(:site\_id=\>params[:site\_id])” or  
> “article\_path(@article, :site\_id=\>params[:site\_id])”.

I don’t know the answer but if you are simply passing it backwards and  
forwards in order to persist it then you might be better off using the  
session. (session[:site\_id] = …)

Colin

---

<div class="post-metadata">

**Author:** ![sigma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@sigma](https://www.ruby-forum.com/u/sigma)\
**Post date:** [July 7, 2010, 10:28pm UTC](https://www.ruby-forum.com/t/passing-parameter-automatically-to-links/191226/3 "2010-07-07T22:28:59Z")

</div>

Hi

thanks, but this is not really what i’m looking for.  
I looking for a solution to (re-)pass a given parameter to all rails  
generated links.

sigma

---

<div class="post-metadata">

**Author:** ![sigma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@sigma](https://www.ruby-forum.com/u/sigma)\
**Post date:** [July 8, 2010, 7:18am UTC](https://www.ruby-forum.com/t/passing-parameter-automatically-to-links/191226/4 "2010-07-08T07:18:02Z")

</div>

Hi

Ok, sessions seems to be right, but not what I’m looking for 🙂

This parameter is not static, it is dynamic. the whole thing is for a  
CMS where the “:site-id” tells where the user stands in the navigation  
and what’s the site title,… So it would be much better to pass this  
by url.

sigma

---

<div class="post-metadata">

**Author:** ![sigma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@sigma](https://www.ruby-forum.com/u/sigma)\
**Post date:** [July 7, 2010, 10:44pm UTC](https://www.ruby-forum.com/t/passing-parameter-automatically-to-links/191226/5 "2010-07-07T22:44:32Z")

</div>

[Please quote when replying.]

Christoph Thommen wrote:

> Hi
> 
> thanks, but this is not really what i’m looking for.  
> I looking for a solution to (re-)pass a given parameter to all rails  
> generated links.

And why won’t a session variable do the trick? It seems to me that  
Colin is right.

You _could_ override some of the UrlWriter functions, but you really,  
really want a session variable, unless there’s something I don’t  
understand here.

> sigma

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![sigma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@sigma](https://www.ruby-forum.com/u/sigma)\
**Post date:** [July 8, 2010, 9:44am UTC](https://www.ruby-forum.com/t/passing-parameter-automatically-to-links/191226/6 "2010-07-08T09:44:58Z")

</div>

On 8 July 2010 06:16, sigma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi
> 
> Ok, sessions seems to be right, but not what I’m looking for 🙂
> 
> This parameter is not static, it is dynamic. the whole thing is for a  
> CMS where the “:site-id” tells where the user stands in the navigation  
> and what’s the site title,… So it would be much better to pass this  
> by url.

I would suggest that if you are using it for context information  
(where the user is now) then the session is exactly the right place to  
put it. Just update the session each time the value changes. A URL  
is for indicating what to do next, not passing where you are at the  
moment.

By the way it is generally preferred not to top post on this list, it  
is easier to follow a thread if you insert your comments inline in the  
previous message. Thanks.

Colin

---

<div class="post-metadata">

**Author:** ![sigma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@sigma](https://www.ruby-forum.com/u/sigma)\
**Post date:** [July 8, 2010, 11:09am UTC](https://www.ruby-forum.com/t/passing-parameter-automatically-to-links/191226/7 "2010-07-08T11:09:17Z")

</div>

On Jul 8, 9:43 am, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Colin

Hi

I think you didn’t understand what I mean…  
Every Controller is something like a module in this cms. so you can  
put such a module on different places on the website.

so you have a navigation, which points to a module (contoller) and a  
certain place, where this module is placed within the navigation.  
In this case the URL should also contain the site-id (navigation-id)  
in order to find out which content of the module is part of this  
navigation item. So you can’t pass this via session.

For example:  
we habe a news-module which is placed on the home page (site\_id = 1)  
and under “about \> blog” (site\_id = 99).  
so the url would look like this:  
[www.example.com/1/news](http://www.example.com/1/news) for the home page and [www.example.com/99/news](http://www.example.com/99/news)  
for the “about \> blog” page.

and this you can’t pass by session.

sigma

---

<div class="post-metadata">

**Author:** ![sigma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@sigma](https://www.ruby-forum.com/u/sigma)\
**Post date:** [July 8, 2010, 12:14pm UTC](https://www.ruby-forum.com/t/passing-parameter-automatically-to-links/191226/8 "2010-07-08T12:14:51Z")

</div>

On Jul 8, 11:48 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On Jul 7, 5:21 pm, sigma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hi there
> 
> > Is it possible to pass a parameter automatically to every rails  
> > generated link?
> 
> ActionController::default\_url\_options ?
> 
> Fred

Hi Fred

This is exaclty what I’m looking for!  
Thank you

---

<div class="post-metadata">

**Author:** ![sigma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@sigma](https://www.ruby-forum.com/u/sigma)\
**Post date:** [July 8, 2010, 11:49am UTC](https://www.ruby-forum.com/t/passing-parameter-automatically-to-links/191226/9 "2010-07-08T11:49:41Z")

</div>

On Jul 7, 5:21 pm, sigma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi there
> 
> Is it possible to pass a parameter automatically to every rails  
> generated link?

ActionController::default\_url\_options ?

Fred
