Ruby-on-rails Rails 方式 - 命名空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/119197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The Rails Way - Namespaces
提问by Carlos
I have a question about how to do something "The Rails Way". With an application that has a public facing side and an admin interface what is the general consensus in the Rails community on how to do it?
我有一个关于如何做“Rails 方式”的问题。对于具有面向公众的一面和管理界面的应用程序,Rails 社区对如何做到这一点的普遍共识是什么?
Namespaces, subdomains or forego them altogether?
命名空间、子域还是完全放弃它们?
回答by Ben Scofield
There's no real "Rails way" for admin interfaces, actually - you can find every possible solution in a number of applications. DHH has implied that he prefers namespaces (with HTTP Basic authentication), but that has remained a simple implication and not one of the official Rails Opinions.
实际上,管理界面没有真正的“Rails 方式”——您可以在许多应用程序中找到所有可能的解决方案。DHH 暗示他更喜欢命名空间(使用 HTTP 基本身份验证),但这仍然是一个简单的暗示,而不是正式的 Rails 意见之一。
That said, I've found good success with that approach lately (namespacing + HTTP Basic). It looks like this:
也就是说,我最近发现这种方法取得了很好的成功(命名空间 + HTTP Basic)。它看起来像这样:
routes.rb:
路线.rb:
map.namespace :admin do |admin|
admin.resources :users
admin.resources :posts
end
admin/users_controller.rb:
管理员/users_controller.rb:
class Admin::UsersController < ApplicationController
before_filter :admin_required
# ...
end
application.rb
应用程序.rb
class ApplicationController < ActionController::Base
# ...
protected
def admin_required
authenticate_or_request_with_http_basic do |user_name, password|
user_name == 'admin' && password == 's3cr3t'
end if RAILS_ENV == 'production' || params[:admin_http]
end
end
The conditional on authenticate_or_request_with_http_basictriggers the HTTP Basic auth in production mode or when you append ?admin_http=trueto any URL, so you can test it in your functional tests and by manually updating the URL as you browse your development site.
authenticate_or_request_with_http_basic在生产模式下或附加?admin_http=true到任何 URL 时,条件 on会触发 HTTP 基本身份验证,因此您可以在功能测试中对其进行测试,并在浏览开发站点时手动更新 URL。
回答by psst
In some smaller applications I don't think you need to separate the admin interface. Just use the regular interface and add admin functionality for logged in users.
在一些较小的应用程序中,我认为您不需要分离管理界面。只需使用常规界面并为登录用户添加管理功能。
In bigger projects, I would go with a namespace. Using a subdomain doesn't feel right to me for some reason.
在更大的项目中,我会使用命名空间。出于某种原因,使用子域对我来说并不合适。
回答by Carlos
Thanks to everyone that answered my question. Looks like the consensus is to use namespaces if you want to as there is no DHH sponsored Rails Way approach. :)
感谢所有回答我问题的人。看起来共识是如果你想使用命名空间,因为没有 DHH 赞助的 Rails Way 方法。:)
Again, thanks all!
再次感谢大家!
回答by Adit Saxena
Its surely late for a reply, but i really needed an answer to this question: how to easily do admin areas?
回复肯定迟到了,但我真的需要回答这个问题:如何轻松地做管理区域?
Here is what can be used these days: Active Admin, with Ryan Bates's great intro.
以下是这些天可以使用的内容:Active Admin,以及Ryan Bates 的精彩介绍。

