Rails 3路由-如何使用作用域创建管理员前缀 [英] Rails 3 Routing - How to use scope to create admin prefix

查看:110
本文介绍了Rails 3路由-如何使用作用域创建管理员前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用本Rails指南创建范围,以便为某些对象创建 / admin前缀

I'm using this Rails Guide to create a scope in order to create an "/admin" prefix for some controllers.

所以我有一个名为Pages的控制器,我想通过 / admin / pages访问它。

So I have a controller named Pages, I want to access it via "/admin/pages".

scope "/admin" do
    resources :pages
end

这很好用,但是仍然可以通过 / pages访问...如何防止这种情况发生? (我正在使用Rails 3)

That works great, but it is still accessible via "/pages" ... How do I prevent that? (I'm using Rails 3)

这是我的路线文件:

devise_for :users

scope "/admin" do

    resources :pages

    resources :contents

end

root :to => "index#index"   

match ':controller(/:action(/:id(.:format)))'


推荐答案

您的命名空间语法正确,但是您需要从最后一行删除所有捕获的匹配项,因为根据默认设置route.rb文件,

Your syntax for the namespace is correct, but you need to remove the catch-all match from the last line because, according to the default routes.rb file,

# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.

如果所请求的URL与您声明的名称空间不匹配,它将仍然与catch-所有路线都在末尾。

If the requested URL does not match the namespace you have declared, it will still match against the catch-all route at the end.

这篇关于Rails 3路由-如何使用作用域创建管理员前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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