【发布时间】:2011-11-26 04:08:06
【问题描述】:
我在我的应用中添加了一个 Admin 命名空间,所以当登录到管理区域时,它必须是这样的:admin/websites 和 admin/page/8
这就是我在 routes.rb 中的内容
namespace :admin do |admin|
match '/' => 'dashboard#index'
resources :websites
resources :pages
resources :sessions
get 'login' => 'sessions#new', :as => 'login'
get 'logout' => 'sessions#destroy', :as => 'logout'
end
我在 app/controllers 目录中有 admin_controller.rb。
class Admin::BaseController < ApplicationController
protect_from_forgery
include UrlHelper
...
我在 app/controllers 中创建了一个管理目录。所以我在 app/controllers/admin/websites_controller.rb 里面有这个
class Admin::WebsitesController < ApplicationController
其他一些答案建议类 Admin::WebsitesController
那么在我的布局文件 (app/views/layouts/application.html.erb) 中,我有这样的链接 edit_admin_website_path(@website) 给我路由错误 Routing Error No route matches {:action=>"edit", :controller=>"admin/websites"}Whyyyy?! :(
【问题讨论】:
-
这个错误返回的 url 选项通常应该是
{:action=>"edit", :controller=>"admin/websites", :id => 3}或任何 id,你能检查一下:@website不是 nil 并且@website是持久的(不是new_record?) -
你使用的是什么版本的 Rails?
-
一个巧妙的技巧:如果您发现
edit_admin_website_path(@website)过于冗长,您可以使用(至少对于 url / 链接助手):[:edit, :admin, @website] -
你是对的!我对 application_controller 进行了更改,并且不再加载 @website。所以我刚刚创建了一个 current_website 辅助方法并在 edit_admin_website_path 上使用它,就像这样 edit_admin_website_path(current_website) 非常感谢! :D
标签: ruby-on-rails ruby namespaces routes