Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 549
xaph Avatar asked Nov 09 '22 20:11

xaph


1 Answers

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.

like image 70
Tim Kretschmer Avatar answered Nov 15 '22 05:11

Tim Kretschmer