自定义子域上的 Rails 命名空间管理员 [英] Rails namespace admin on custom subdomain

查看:49
本文介绍了自定义子域上的 Rails 命名空间管理员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 rails 应用程序设置为使用此 RailsCast 中所述的子域:

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

但它不是通过我的 app/controllers/admin/blogs_controller 进行路由,而是尝试通过我的普通用户"控制器 (app/controllers/blogs_controller) 进行路由.

But instead of routing through my app/controllers/admin/blogs_controller it attempts to route through my "normal user" controller (app/controllers/blogs_controller).

我是否只是遗漏了一些简单的东西,或者在 Rails 中做这样的事情要困难得多?

Am I just missing something simple or is doing something like this in rails much more difficult?

推荐答案

我能够解决这个问题,尽管它感觉有点hackish.了解 Rails 将约束处理为真或假,我在初始子域约束检查中设置了另一个约束.它将子域拆分为 2 并检查第一个子域以查看它是否等于admin".如果为 true,则路由到 admin/controllers 和 admin/views(因为 module: "admin"),如果不是,则路由到不在admin"模块内的不太具体的路由.

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.

起初我没有命名空间:admin,而且我的路由助手不正确(管理路由没有以admin"为前缀,也没有设置不太具体的路由因为它们是重复的).一旦我添加了 namespace :admin 和路径:""(这也很重要,因为它从 URI 模式中删除了admin/"),它起作用了!

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!

最后一件事,在 admin/controllers 中,您必须编辑 set_blog 方法,因为admin.company"正在被解释(参见 admin/blogs_controller.rb).

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

耙路线:

Prefix Verb URI Pattern Controller#Action
 admin GET  /   admin/blogs#show
 ...

获取/博客#show...

​ GET / blogs#show ...

admin/blogs_controller.rb

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.

这篇关于自定义子域上的 Rails 命名空间管理员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆