Rails 3 Routing

I have a community_users model that I route as follows:

resources :communities do
  resources :users
end

This creates a route /communities/:id/users/.

I would like to configure this route so that only the community name is displayed with the corresponding: id.

In other words, if the community has the identifier "1" and the name "rails-lovers", the route will read:

/rails-lovers

and not:

/communities/1/users/
+3
source share
2 answers

You might want to check out the friendly_id gem

This will give you the clean URLs you are looking for.

+3
source

I'm not quite sure if this is what you are looking for, but:

One option is to create a route

match ':community_name' => 'users#show_users_for_community'

and then in UserController

def show_users_for_community
  @community = Community.find_by_name(params[:community_name])
  <do what you need to do here>
end

, URL- - . , , , .

+3

All Articles