@
@
@Quty2015-05-09 04:02:17
Ruby on Rails
@Quty, 2015-05-09 04:02:17

How to achieve correct subdomain routing in Rails 4?

I'm trying to highlight the 'admin' subdomain in an application. The command rails g controller Admin::Userscreated the necessary controller in the Admin namespace, it turned out like this:

class Admin::UsersController < Admin::ApplicationController
  
  def index
    @users = User.all
  end
  
  def show
    @user = User.find(params[:id])
  end
  
end

The Admin::ApplicationController itself is the following:
class Admin::ApplicationController < ActionController::Base
  http_basic_authenticate_with name: "admin", password: "secret"
  layout "admin/application"
end

The config/routes.rbprescribed
TestApp::Application.routes.draw do
# ...
  namespace :admin do
    constraints subdomain: "admin" do
      resources :users
    end
  end
#...
end

rake routesissues
admin_users GET    /admin/users(.:format)          admin/users#index {:subdomain=>"admin"}
                POST   /admin/users(.:format)          admin/users#create {:subdomain=>"admin"}
 new_admin_user GET    /admin/users/new(.:format)      admin/users#new {:subdomain=>"admin"}
edit_admin_user GET    /admin/users/:id/edit(.:format) admin/users#edit {:subdomain=>"admin"}
     admin_user GET    /admin/users/:id(.:format)      admin/users#show {:subdomain=>"admin"}
                PATCH  /admin/users/:id(.:format)      admin/users#update {:subdomain=>"admin"}
                PUT    /admin/users/:id(.:format)      admin/users#update {:subdomain=>"admin"}
                DELETE /admin/users/:id(.:format)      admin/users#destroy {:subdomain=>"admin"}

Routing to the URL http://admin.lvh.me:3000/admin/users/works fine, but in this case 'admin' is duplicated in the path. In an attempt to get rid of this, I replaced it with In this case , it gives a seemingly sane result.
namespace :admin do
namespace :admin, path: '' do
rake routes
admin_users GET    /users(.:format)          admin/users#index {:subdomain=>"admin"}
                POST   /users(.:format)          admin/users#create {:subdomain=>"admin"}
 new_admin_user GET    /users/new(.:format)      admin/users#new {:subdomain=>"admin"}
edit_admin_user GET    /users/:id/edit(.:format) admin/users#edit {:subdomain=>"admin"}
     admin_user GET    /users/:id(.:format)      admin/users#show {:subdomain=>"admin"}
                PATCH  /users/:id(.:format)      admin/users#update {:subdomain=>"admin"}
                PUT    /users/:id(.:format)      admin/users#update {:subdomain=>"admin"}
                DELETE /users/:id(.:format)      admin/users#destroy {:subdomain=>"admin"}

But routing by the view URL http://admin.lvh.me:3000/users/:id/[edit]directs the request not to Admin::Users, but to Users.
Rails 4.0.2, Ruby 2.0.0p643

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jeiwan, 2015-05-09
_

Something like this:

constraints subdomain: 'admin' do
  scope module: 'admin', as: 'admin' do
    resources :users
  end
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question