Altering URLs and mapping - path_prefix? - Ruby on Rails...
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
16:27 UTC
Read the original article
Hit count: 480
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 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 which blows up my view because nothing is being set for the @user variable.
How do I tell Rails to create the link reading www.myapp.com/brandon/posts/id vs www.myapp.com/posts/id and then map that action?
I am assuming this will involve some code in the view, and then adding another line in the routes.rb file,
map.subdomain_link '/:username/posts/:id', :controller => 'posts', :action => 'show'
and adding the @user variable to the PostController#show action, but not totally sure if this is even the right approach.
UPDATE: I have been reading about :path_prefix and seems like it might be what I am looking for. I have tried to implement simply by adding
map.resources :posts, :path_prefix => '/:user_username/:post_id'
to the routes.rb file but not working (even after server restart). I am sure this is not correct but wanted to let you know what I have tried.
© Stack Overflow or respective owner