简体   繁体   中英

How to get resource id from subdomain in Rails 4?

We're developing a conference management app and most of the resources is nested resource of conference. Now, we decided to use subdomains for conference homepages and got trouble on refactoring resources.

Current url scheme is like:

/conferences/:id/speeches

/conferences/:id/manage

We want to move /conferences/:id part to subdomain and use resources like:

conferenceid.sitename.com/speeches

conferenceid.sitename.com/manage

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

What's the best way to make this transition? How can we prevent current url helpers?

first lets tweak your subdomain class. the following code should be enough

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

then you should be able to call it in the 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 go like this:

Conference.find_by_slugged!(request.subdomain)

(i saw you use friendly id, so i think your subdomain is sluggged of the Conference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM