Rails嵌套资源的子域

时间:2015-08-06 15:05:03

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 subdomain rails-routing

我有一个Rails 4应用,我尝试重新配置为jobportals使用子域名。现在,每个门户的根路径是/jobportals/:id。我想要的是让用户能够转到client.example.com并点击该根路径。然后,例如,如果用户修改了他们的个人资料,则网址将为client.example.com/user/edit而不是www.example.com/jobportals/:id/user/edit

我按照this ASCIIcasts tutorial设置了子域路由,但它对我不起作用。当我转到http://client.lvh.me:3000/时,我正在点击我的Rails应用的root_path,而不是门户的root_path

我认为问题在于Constraints(Subdomain)不能与resources :jobportals合作。如何重新配置​​我的路线以完成我之后的工作?

MyApp::Application.routes.draw do
    devise_for :users, :controllers => {:registrations => "registrations"}
    root 'general#home'

    get '/about' => 'general#about'
    get '/team' => 'general#team'
    get '/careers' => 'general#careers'
    get '/terms' => 'general#terms'
    get '/privacy_policy' => 'general#privacy_policy'

    constraints(Subdomain) do
        resources :jobportals, controller: 'portals/general' do
            member do
                root 'portals/general#home'
                devise_scope :user do
                    get '/user/sign_in' => 'portals/sessions#new'
                    post '/user/sign_in' => 'portals/sessions#create'
                    delete '/user/sign_out' => 'portals/sessions#destroy'
                    post '/user/password' => 'portals/passwords#create'
                    get '/user/password/new' => 'portals/passwords#new'
                    get '/user/password/edit' => 'portals/passwords#edit'
                    patch '/user/password' => 'portals/passwords#update'
                    put '/user/password' => 'portals/passwords#update'

                    post '/user' => 'portals/registrations#create'
                    get '/user/sign_up' => 'portals/registrations#new'
                    get '/user/edit' => 'portals/registrations#edit'
                    patch '/user' => 'portals/registrations#update'
                    put '/user' => 'portals/registrations#update'
                    delete '/user' => 'portals/registrations#destroy'
                end

                get '/jobs' => 'portals/general#jobs'
                get '/companies' => 'portals/general#companies'
                get '/alljobs' => 'portals/general#alljobs'

                resources :applications, controller: 'portals/applications'

                get ':id' => 'portals/companies#profile'
                get ':id/jobs' => 'portals/companies#jobs'
                get ':id/jobfunctions' => 'portals/companies#jobfunctions'
            end
        end
    end
end

下面的工作代码

MyApp::Application.routes.draw do
    devise_for :users, :controllers => {:registrations => "registrations"}
    root 'general#home'

    get '/about' => 'general#about'
    get '/team' => 'general#team'
    get '/careers' => 'general#careers'
    get '/terms' => 'general#terms'
    get '/privacy_policy' => 'general#privacy_policy'

    constraints(Subdomain) do
        get '/' => 'portals/general#home'
        devise_scope :user do
            get '/user/sign_in' => 'portals/sessions#new'
            post '/user/sign_in' => 'portals/sessions#create'
            delete '/user/sign_out' => 'portals/sessions#destroy'
            post '/user/password' => 'portals/passwords#create'
            get '/user/password/new' => 'portals/passwords#new'
            get '/user/password/edit' => 'portals/passwords#edit'
            patch '/user/password' => 'portals/passwords#update'
            put '/user/password' => 'portals/passwords#update'

            post '/user' => 'portals/registrations#create'
            get '/user/sign_up' => 'portals/registrations#new'
            get '/user/edit' => 'portals/registrations#edit'
            patch '/user' => 'portals/registrations#update'
            put '/user' => 'portals/registrations#update'
            delete '/user' => 'portals/registrations#destroy'
        end

        get '/jobs' => 'portals/general#jobs'
        get '/companies' => 'portals/general#companies'
        get '/alljobs' => 'portals/general#alljobs'

        resources :applications, controller: 'portals/applications'

        get ':id' => 'portals/companies#profile'
        get ':id/jobs' => 'portals/companies#jobs'
        get ':id/jobfunctions' => 'portals/companies#jobfunctions'
    end
end

1 个答案:

答案 0 :(得分:0)

看起来约束没有正确定义,它类似于get 'jobportals', constraints: {subdomain: 'subdomain_name'}     Checkout the rails guide