Rails 2 Namespace and Shallow Routes Issue

浪子不回头ぞ 提交于 2019-12-12 09:27:53

问题


I've written the admin area of a particular rails app and now I'm ready to set it as own section within the website.

Therefore, it will be /admin

However, I didn't want to have it as /admin within the route itself I wanted to have something less common, so I added a couple of hyphens before and after it.

So the route is /-admin-/ and the namespace is Admin.

After setting this up using :path_prefix => "/-admin-", I have the following code block:

map.namespace "/-admin-/", :name_prefix => "", :path_prefix => "/-admin-" do |admin|

This works for all but shallow routes, instead, in the rake routes output the output is:

new_page GET    /-admin-/areas/:area_id/pages/new(.:format)                         {:action=>"new", :controller=>"admin/pages"}
edit_admin_page GET    /admin/pages/:id/edit(.:format)                                     {:action=>"edit", :controller=>"admin/pages"}
admin_page GET    /admin/pages/:id(.:format)                                          {:action=>"show", :controller=>"admin/pages"}
PUT    /admin/pages/:id(.:format)                                          {:action=>"update", :controller=>"admin/pages"}
DELETE /admin/pages/:id(.:format)                                          {:action=>"destroy", :controller=>"admin/pages"}
areas GET    /-admin-/areas(.:format)                                            {:action=>"index", :controller=>"admin/areas"}
POST   /-admin-/areas(.:format)                                            {:action=>"create", :controller=>"admin/areas"}
new_area GET    /-admin-/areas/new(.:format)                                        {:action=>"new", :controller=>"admin/areas"}

Notice how the shallow-routed routes are prefixed as /admin/ and not as /-admin-/ (as are their parent routes).

Any ideas on how to get around this? Is this a bug in rails or do I need to work around it? I tried adding the :path_prefix to each nested route but it doesn't do anything?

Any ideas?


回答1:


I'm not sure about your rationale on not using /admin - security through obscurity isn't really security - you should be using something like authlogic to keep out unauthorised users.

Try the following to namespace your admin controllers:

map.namespace :admin, :path_prefix => "-admin-" do |admin|
    admin.resources :users
    admin.resources :pages
end

A sample generated route:

admin_users GET /-admin-/users(.:format) {:controller=>"admin/users", :action=>"index"}



回答2:


There is no way to get around this. Turns out that all versions of Rails will break down the URL and its resource names to their lowest points when they're set to shallow. The only solution to this is to set all of your resource routes manually without using map.resources.



来源:https://stackoverflow.com/questions/3146659/rails-2-namespace-and-shallow-routes-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!