Home Rails: Configure routes to attempt to find records from two classes
Reply: 1

Rails: Configure routes to attempt to find records from two classes

jonhue
1#
jonhue Published in 2018-01-12 19:15:30Z

This is my current route configuration:

resources :organizations, path: ''
resources :users, path: ''

I want to create a similar experience to what GitHub does. When using GitHub, you can access organization and user profile pages by entering "https://github.com/#{username}"

Now, the routes configuration above leads to the obvious problem that accessing organizations works fine while accessing a user fails because Rails only considers the organizations route and does not attempt to find a user.

Note: I am using friendly id to use usernames in my URL's and also made sure that usernames are unique across both ActiveRecord classes.

How do I do what I want to do?

Sergey Sokolov
2#
Sergey Sokolov Reply to 2018-01-12 20:03:45Z

You can create additional controller like PageOwnerController and pass request to it:

get ':page_owner_nick', to: 'page_owners#show', as: :page_owner

In show action you can manually find desired record by params[:page_owner_nick].

Advice: it looks like you have many a lot of similar logic between users and organizations - take a look on STI. Using STI allow you to write common code easier, but at the same time to separate different logic.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.306721 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO