add subdomain if namespace present - ruby-on-rails

Not sure if my topic title is correct, but here is my question
I have namespace called :admin, so it looks like mysite.com/admin. In this section i have some links, that pointing to controllers inside this namespace. But since we have subdomain admin, and my namespace :admin as well, i'd like to all links that are being generated by routes.rb to prepend string admin., so the link would look like admin.mysite.com/admin/some_other_path
I've tried to add constraints to routes.rb, but that didn't work for me

But since we have subdomain admin, and my namespace :admin as well,
i'd like to all links that are being generated by routes.rb to prepend
string admin.
Routes
In your routes, you should have this:
constraints({ subdomain: "admin" }) do
namespace :admin do
# routes here
end
end
If you wanted to have no path for your admin namespace (I.E admin.domain.com/some_other_path), you can do this:
constraints({ subdomain: "admin" }) do
namespace :admin, path: "" do
# routes here
end
end
--
URL
When using URLs, you have to use the _url helpers (not _path). We literally just discovered this yesterday - the _path helpers only work to append relative paths to your url; the _url gives you a totally fresh url
This means if you have a route as follows:
admin_root_path "admin/application#index, constraints => {subdomain: "admin"}
You'll call this with this route helper:
<%= link_to "Admin", admin_root_url %>
This will prepend the required subdomain for you when calling links etc

you can do:
constraints subdomain: 'admin' do
namespace :admin do
# ...
end
end

Define routes in routes.rb under admin namespace like this
namespace :admin, path: '/', constraints: { subdomain: 'admin' } do
constraints(Subdomain) do
# your routes
end
end
Routes defined under this block will always come under admin in link, e.g., /admin/some_other_path
To add subdomain to the admin namespace take a look at this question
Rails namespace admin on custom subdomain

Related

how to make dynamic namespace or scopes route in rails

I am trying to make admin route with namespace but it doest trigger to the route
I run rails g controller admin
it created the file app/controllers/admin_controller.rb , app/views/admin/index.html.haml
my namespace look like this
namespace :admin do
controller :admin do
get '/', :index
end
end
it doesn't trigger to localhost:3000/admin it said not found for that route
any idea ??
namespace not only adds a path prefix but it also adds a module nesting to the expected controller. For example:
namespace :blog do
resources :posts
end
Will create the route /blog/posts => Blog::PostsController#index.
In your example Rails expects the controller to be defined as:
# app/controllers/admin/admin_controller.rb
module Admin
class AdminController
# GET /admin
def index
end
end
end
If you just want to add a path prefix or other options to the routes without module nesting use scope instead:
scope :admin, controller: :admin do
get '/', action: :index
end
This will route /admin to AdminController#index
But if this just a one off route you could just write:
get :admin, to: 'admin#index'
Your controller path needs to be in the admin namespace.
So the filename for the admin controller needs to be app/controllers/admin/admin_controller.rb.

Rails 4 subdomain routing

I have the following in my routes.rb file
namespace :client, path: '/' do
get '/' => 'default#index', as: :default_index
end
namespace :api, path: '/', constraints: { subdomain: 'api' } do
get '/' => 'default#index', as: :default_index
end
I have mounted a subdomain call "test.dev", and I can access "test.dev" without problems. However, if I try to get "api.test.dev" I get the same page as in "test.dev", unless I change the path for the namespace with the subdomain constraint.
Can't I have the same path for both even though one is on a subdomain and the other one isn't?
#Rodrigo, I've just faced the same problem, and I figure out the solution \o/.
The problem is in your "api.test.dev". I'm not dive into the details of why's, but it should be "api.test-dev.com", so append ".com" to your etc/hosts and change it to be like that:
api.test.dev
to
api.test-dev.com

Rails namespace admin on custom subdomain

My rails app is set to use subdomains as described in this RailsCast:
http://railscasts.com/episodes/221-subdomains-in-rails-3
Now, I would like to add an admin subdomain to the front of my blog subdomain as follows:
admin.company.lvh.me:3000
I've tried to namespace admin outside of my Subdomain constraint:
namespace :admin, path: '/', constraints: { subdomain: 'admin' } do
constraints(Subdomain) do
match '/', to: 'blogs#show', via: 'get'
end
end
But instead of routing through my app/controllers/admin/blogs_controller it attempts to route through my "normal user" controller (app/controllers/blogs_controller).
Am I just missing something simple or is doing something like this in rails much more difficult?
I was able to solve this, although it feels a little hackish. Understanding that Rails treats constraints either true or false, I set another constraint inside the initial subdomain constraint check. It splits the subdomain in 2 and examines the first subdomain to see if it equals "admin". If true, it routes to the admin/controllers and admin/views (because of module: "admin"), if not, it routes to the less specific routes that are not inside the "admin" module.
At first I didn't have the namespace :admin, and my route helpers were incorrect (the admin routes weren't prefixed with "admin" and the less specific routes weren't being set since they were duplicates). Once I added namespace :admin and the path: "" (this is important, too, because it removes "admin/" from the URI Pattern), it worked!
One last thing, in the admin/controllers, you have to edit the set_blog method, since "admin.company" is being interpreted instead (see admin/blogs_controller.rb).
routes.rb
Blog::Application.routes.draw do
constraints(Subdomain) do
namespace :admin, module: "admin", path: "", constraints: lamda { |r| r.subdomain.split('.')[0] == 'admin' } do
match '/', to: 'blogs#show', via: 'get'
...
end
match '/', to: 'blogs#show', via: 'get'
...
end
...
end
Rake Routes:
Prefix Verb URI Pattern Controller#Action
admin GET / admin/blogs#show
...
​ GET / blogs#show
...
admin/blogs_controller.rb
BlogController < ApplicationController
before_action :set_blog
...
private
set_blog
#blog = Blog.find_by_subdomain!(request.subdomain.split('.')[1])
end
end
Let me know if there's anything cleaner out there, if not, hopefully this helps others with this issue.
There are several important factors here
Firstly, you'll need to see what the constraint params look like with "multi" subdomains. Instead of splitting, Rails may have admin.company as the subdomain
If we take the idea that Rails will split the subdomains into two, which one is being called as the "parent"?
namespace :admin, path: '/', constraints: { subdomain: 'admin' } do
constraints(Subdomain) do
resources :blogs, only: :show, path_names: { show: "" }
end
end
If you give us some more info on the request (params etc), we'll be in a much better position to help!

Adding prefix to a named route helper under namespace

This is how a common namespace looks like.
namespace :admin do
resources :posts
end
And it creates a named route like this one;
new_admin_post_path
Here's my question; how can I add a prefix (like "new" in this example) to a named route under namespace?
Let's say my route definition likes this one;
namespace :admin do
get 'post/new' => 'posts#new', as: 'post'
end
And it's creates a named route like;
admin_post_path
I want to add "new" prefix to this named route and make it look like new_admin_post_path and I don't want to use resources.
Just try the code in routes.
namespace :admin, as: '' do
get '/post/new' => 'posts#new', as: 'new_admin_post'
end
If you don't want to make admin namespace as nil, then you can do it. for that you need to put that route out of the namespace :admin block in routes
namespace :admin do
# your other routes
end
get '/admin/post/new' => 'admin/posts#new', :as => 'new_admin_post'

Prefix url helpers in subdomain constraints without having the prefix in the URL

Here is my routes.rb file :
constraints subdomain: 'pro' do
scope module: 'pro' do
resources :subjects
end
end
This will generate:
subjects GET /subjects(.:format) pro/subjects#index {:subdomain=>"pro"}
How can I have the pro_subject url helper without having the '/pro' prefix in the URL as I already have the subdomain.
I want people to type: http://pro.mysuperwebsite.com and not http://pro.mysuperwebsite.com/pro
I'm pretty sure if you set the :path option, you can determine the subpath.
namespace :pro, :path => '' do # This should do it

Resources