Inserting variable into routes - map.resources :posts, :as => X - is this possible?

Posted by bgadoci on Stack Overflow See other posts from Stack Overflow or by bgadoci
Published on 2010-04-30T15:51:29Z Indexed on 2010/04/30 19:27 UTC
Read the original article Hit count: 539

Ok, so I am working on a blog application of sorts. Thus far, it allows for a user to sign up for their own account, create posts, tags, comments, etc.

I have just implemented the ability to use www.myapp.com/brandon to set @user to find by username and therefore correctly display the users information at each url. So when you go to www.myapp.com/brandon you see all Brandon's posts, tags, and comments associated with those posts, etc. Works great.

I'm implementing this URL mapping through the routes.rb file by adding the following:

map.username_link '/:username', :controller => 'posts', :action => 'index'

And then just setting the @user variable in the PostController and corresponding views to find_by_username. Now the issue is this. Once at www.myapp.com/brandon when you click on a post title, it sends to myapp.com/posts/id without the username in the URL.

How do I tell rails to replace the /posts with /username.

Is it even possible to insert the user_username variable into this code?

map.resources :posts, :as => [what goes here] 

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby