views:

452

answers:

3

Hello,

I'm not exactly sure how to frame this question so I'll just go for it. I am recently returning to rails after a hiatus and I am building a social networking style application. Basically I'd like to do what twitter does and have each persons profile page be found at "http://www.url.com/username" and while I have managed to get this partially to work.... if I manually type this into the address bar it takes me to their profile page, however I can't seem to figure out how to get my links to here working. I'm sure it has something to do with my routes, but I've never come across any thorough enough routes tutorials to understand this aspect well enough to pull it off.

Here's the code in my routes.rb

map.connect '/:username', :controller => 'users', :action => 'show'

Beyond that i'm at a loss how to make links from various other parts of the application (I've got a User, Question, Friendship, Answer and Votes model at this point.

If sorry that I am unable to frame this question better, but it's one of the main reasons this application isn't getting further. Please let me know if you can point me in the right direction.

Thanks yo!

Oh yeah, PS I'd like to be able to add the questions and answers on top of the URL, such as:

http://www.url.com/username/question/answer/2210

If such a thing is possible

+4  A: 

There's nothing wrong with your route. Just remember to define it at the end, after defining all other routes. I would also recommend using RESTful routes and only if you want to have better looking URLs use named routes. Don't use map.connect. Here's some good reading about Rails routes.

Here's how this could look:

map.resources :questions, :path_prefix => '/:username' do |question|
  question.resources :answers
end

map.resources :users

map.user '/:username', :controller => 'users', :action => 'show'

Just a draft you can extend.

Paweł Gościcki
+1  A: 

To create urls you need to define to_param method for your user model (read here).

class User < ActiveRecord::Base
  def to_param 
    username
  end
end
klew
A: 

I have used like this

In View part <%= link_to portfolio.name, show_portfolio_path(:username=>portfolio.user.name,:id =>portfolio) %>

and in rout.rb

map.show_portfolio "portfolios/:username", :action => 'show_portfolio', :controller => 'portfolios'

sonawane