简体   繁体   English

如何从Rails 4中的子域获取资源ID?

[英]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 /会议/:ID /演讲

/conferences/:id/manage /会议/:ID /管理

We want to move /conferences/:id part to subdomain and use resources like: 我们想将/ meetings /:id部分移动到子域并使用以下资源:

conferenceid.sitename.com/speeches conferenceid.sitename.com/speeches

conferenceid.sitename.com/manage conferenceid.sitename.com/manage

Here are the current routes file: https://github.com/kodgemisi/confdeck/blob/development/config/routes.rb#L17 以下是当前路线文件: 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. (我看到你使用友好ID,所以我认为你的子域名在会议中被sluggged了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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