# Map.connect / url re-writing

**URL:** <https://www.ruby-forum.com/t/map-connect-url-re-writing/71813>\
**Category:** Rails\
**Created:** [October 11, 2006, 5:40pm UTC](https://www.ruby-forum.com/t/map-connect-url-re-writing/71813 "2006-10-11T17:40:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![blacknight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@blacknight](https://www.ruby-forum.com/u/blacknight)\
**Post date:** [October 11, 2006, 5:40pm UTC](https://www.ruby-forum.com/t/map-connect-url-re-writing/71813/1 "2006-10-11T17:40:03Z")

</div>

Hey everyone,

I’ve got an application that manages users. It has a default layout with  
a navigation bar, whose links require the current user – for example,  
\<% link\_to ‘Contact Details’, :controller =\> ‘user’, :action =\>  
‘details’, :id =\> @user %\>

The problem with this is that I need to define the @user variable for  
every controller action. It’s really redundant. I was wondering if I  
could rewrite my urls using map.connect so that I to embed a user\_id in  
the url. So something like [www.domain.com/username/controller/action](http://www.domain.com/username/controller/action) –  
can I use map.connect to look up the id and pass it to the controller,  
or do I have to look up the id associated with :username manually in the  
layout?

Thanks,  
Ben

---

<div class="post-metadata">

**Author:** ![blacknight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@blacknight](https://www.ruby-forum.com/u/blacknight)\
**Post date:** [October 11, 2006, 6:35pm UTC](https://www.ruby-forum.com/t/map-connect-url-re-writing/71813/2 "2006-10-11T18:35:05Z")

</div>

To re-phrase, I want to do the following in routes.rb:

map.connect ‘:username/:controller/:action’, …

I realize I can then access :username from params[:username], but I was  
wondering if it was possible to instantiate a @user variable from  
params[:username] in one place that’s accessible anywhere?

Thanks,  
Ben

Ben W. wrote:

> The problem with this is that I need to define the @user variable for  
> every controller action. It’s really redundant. I was wondering if I  
> could rewrite my urls using map.connect so that I to embed a user\_id in  
> the url. So something like [www.domain.com/username/controller/action](http://www.domain.com/username/controller/action) –  
> can I use map.connect to look up the id and pass it to the controller,  
> or do I have to look up the id associated with :username manually in the  
> layout?
> 
> Thanks,  
> Ben

---

<div class="post-metadata">

**Author:** ![blacknight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@blacknight](https://www.ruby-forum.com/u/blacknight)\
**Post date:** [October 12, 2006, 1:08am UTC](https://www.ruby-forum.com/t/map-connect-url-re-writing/71813/3 "2006-10-12T01:08:41Z")

</div>

Ben W. wrote:

> I realize I can then access :username from params[:username], but I was  
> wondering if it was possible to instantiate a @user variable from  
> params[:username] in one place that’s accessible anywhere?

Put the following in your application controller:

before\_filter :init\_user

def init\_user  
@user = User.find\_by\_username(params[:username])  
end

---

<div class="post-metadata">

**Author:** ![blacknight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@blacknight](https://www.ruby-forum.com/u/blacknight)\
**Post date:** [October 12, 2006, 1:20pm UTC](https://www.ruby-forum.com/t/map-connect-url-re-writing/71813/4 "2006-10-12T13:20:25Z")

</div>

Since this is now hitting the database every request for user  
information couldn’t you just store the user id in the session when the  
user logs in? Then you can just reference the session when you need the  
id.

---

<div class="post-metadata">

**Author:** ![blacknight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@blacknight](https://www.ruby-forum.com/u/blacknight)\
**Post date:** [October 12, 2006, 5:06am UTC](https://www.ruby-forum.com/t/map-connect-url-re-writing/71813/5 "2006-10-12T05:06:53Z")

</div>

Eric A. wrote:

> Put the following in your application controller:
> 
> before\_filter :init\_user
> 
> def init\_user  
> @user = User.find\_by\_username(params[:username])  
> end

Thank you! That’s exactly what I was looking for.

Cheers,  
Ben

---

<div class="post-metadata">

**Author:** ![blacknight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@blacknight](https://www.ruby-forum.com/u/blacknight)\
**Post date:** [October 12, 2006, 2:37pm UTC](https://www.ruby-forum.com/t/map-connect-url-re-writing/71813/6 "2006-10-12T14:37:38Z")

</div>

RJ:

Yes you could. However, you would have to make sure you refreshed the  
session if the user object changed. The before\_filter approach seems to  
be  
the appropriate method. It also depends on if you’re talking about the  
same  
user on each request.

[del.icio.us/hoganbp](http://del.icio.us/hoganbp) =\> needs to find my information  
[del.icio.us/homer\_simpson](http://del.icio.us/homer_simpson) =\> needs to look up someone else

So it depends on the intended purpose.

---

<div class="post-metadata">

**Author:** ![blacknight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@blacknight](https://www.ruby-forum.com/u/blacknight)\
**Post date:** [October 18, 2006, 10:38am UTC](https://www.ruby-forum.com/t/map-connect-url-re-writing/71813/7 "2006-10-18T10:38:44Z")

</div>

Ben W. wrote:

> Is a single extra query worth worrying over? (I don’t honestly know  
> myself.)

If you get a performance problem optimize then. Don’t worry about it  
until you actually have a problem.

Eric

---

<div class="post-metadata">

**Author:** ![blacknight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@blacknight](https://www.ruby-forum.com/u/blacknight)\
**Post date:** [October 12, 2006, 3:35pm UTC](https://www.ruby-forum.com/t/map-connect-url-re-writing/71813/8 "2006-10-12T15:35:51Z")

</div>

Brian H. wrote:

> RJ:
> 
> Yes you could. However, you would have to make sure you refreshed the  
> session if the user object changed. The before\_filter approach seems to  
> be  
> the appropriate method. It also depends on if you’re talking about the  
> same  
> user on each request.
> 
> [del.icio.us/hoganbp](http://del.icio.us/hoganbp) =\> needs to find my information  
> [del.icio.us/homer\_simpson](http://del.icio.us/homer_simpson) =\> needs to look up someone else
> 
> So it depends on the intended purpose.

Yeah, this isn’t supposed to be a situation where someone is logged in  
– it’s for viewing a user profile, whether it’s yours or not. So I  
don’t think sessions really apply.

Is a single extra query worth worrying over? (I don’t honestly know  
myself.)

Cheers,  
Ben

---

<div class="post-metadata">

**Author:** ![blacknight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@blacknight](https://www.ruby-forum.com/u/blacknight)\
**Post date:** [October 18, 2006, 10:39am UTC](https://www.ruby-forum.com/t/map-connect-url-re-writing/71813/9 "2006-10-18T10:39:14Z")

</div>

@Ben:

An extra query like that isn’t going to hurt. Databases are designed to  
be  
queried. As Eric says below, don’t optimize until you need to. You can  
cluster your backend databases, you can use model caching, there are  
lots of  
options if performance becomes an issue. However, for this… it’s not  
going to matter. I’d be willing to bet you that there are going to be  
other  
areas you’ll need to optimize first 🙂

Good luck!
