How to get resource ID from subdomain in Rails 4?

We are developing a conference management application, and most resources are an embedded conference resource. Now we decided to use subdomains for the conference home page and got problems with refactoring resources.

The current URL scheme is similar:

/ conference /: ID / speech

/ conference /: identifier / management

We want to move / conferences /: the id part to a subdomain and use resources such as:

conferenceid.sitename.com/speeches

conferenceid.sitename.com/manage

Here is the current routes file: https://github.com/kodgemisi/confdeck/blob/development/config/routes.rb#L17

What is the best way to make this transition? How can we prevent current URL helpers?

+5
source share
1 answer

First lets you set up your subdomain class. the following code should be sufficient

class Subdomain def self.matches?(request) request.subdomain.present? && request.subdomain != 'www' end end 

then you should be able to call it on routes with the following

 constraints(Subdomain) do resource :conference, path: "/" do member do get 'apply' post 'apply' => "conferences#save_apply" end end 

Then in your controller you do the following:

 Conference.find_by_slugged!(request.subdomain) 

(I saw that you are using a friendly identifier, so I think your subdomain is sluggged conference.

+1
source

Source: https://habr.com/ru/post/1210896/


All Articles