Aarmora
Aarmora

Reputation: 1153

Rails pseudo subdomain

I'm not really sure what this is even called so if this question has been asked before, please let me know. I wasn't able to find it with any of the ways I was describing it in rails. I'm also fairly new at rails.

I have a website that I currently use for hosting my own tournaments. I want to open this website up for others to host their own tournaments. I'd like to be able to essentially duplicate all the layout and functionality of the website I currently have but it would have the data specific to their tournaments.

So right now I have domain.com/seasons and domain.com/admin, for example. I envision it being domain.com/tournament1/seasons and domain.com/tournament1/seasons.

I have two original reasons for not wanting it as a subdomain for each new tournament. The first is that all the users sign in through openauth and I'd like that sign in to be persistent through all the family of tournaments. The second is that I'm running the app through heroku, which, so far as I've found, doesn't seem to be very friendly with subdomains.

So I guess my questions are...what do I even call this and this the best way to do it? I picture it being some kind of controller within a controller. My main issue is not even being able to describe this well enough to google.

Thanks in advance. Sorry for the convulsion.

Upvotes: 1

Views: 44

Answers (1)

Richard Peck
Richard Peck

Reputation: 76774

As you've already found the solution in the comments, I'll just explain how it works for you:


Routing

When you use routing in an app, you have to remember that Rails has to take the path the user sends, and, through the ActionDispatch::Routing, middleware, assign the request to a specific controller action

The problem you cite is how to direct those requests based on another parameter (in your case the tournament_name

The answer, as discovered, is to use the nested resources functionality in your routes:

#config/routes.rb
resources :tournaments do
    resources :seasons #-> creates domain.com/:tournament_id/seasons
end

This will create a series of routes to basically pass the params[:tournament_id] var to your seasons controller. You can handle it like this:

#app/controllers/seasons_controller.rb
def index
    @tournament = Tournament.find params[:tournament_id]
end

Slugging

The second point I need to make is the standard Rail setup will only allow an ID to be passed through the URL

However, in order to create slugged routes, you'll want to use something to accept data other than the id. The way to do this is to use the friendly_id gem

This will allow you to pass the likes of:

domain.com/tournament_name/seasons

Upvotes: 1

Related Questions