Answer the question
In order to leave comments, you need to log in
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::Users
created 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
class Admin::ApplicationController < ActionController::Base
http_basic_authenticate_with name: "admin", password: "secret"
layout "admin/application"
end
config/routes.rb
prescribedTestApp::Application.routes.draw do
# ...
namespace :admin do
constraints subdomain: "admin" do
resources :users
end
end
#...
end
rake routes
issuesadmin_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"}
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"}
http://admin.lvh.me:3000/users/:id/[edit]
directs the request not to Admin::Users
, but to Users
. Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question