【问题标题】:Devise routing issues with admin role使用管理员角色设计路由问题
【发布时间】:2013-12-27 20:25:37
【问题描述】:

我正在尝试将 Devise 添加到现有的 Rails 3.2.16 应用程序中。

我已经具备基本功能,但我想创建一个管理员角色,无需输入密码即可编辑任何用户配置文件。

我使用Devise Wiki "Add An Admin Role" section选项2 - 添加管理员属性 中的代码来设置管理员角色。

我的 routes.rb 有这个:

  devise_for :users, :controllers => { :registrations => :registrations }

  scope "/admin" do
      resources :users
  end

这给了我以下路线

new_user_session     GET     /users/sign_in(.:format)      devise/sessions#new
user_session         POST    /users/sign_in(.:format)      devise/sessions#create
destroy_user_session DELETE  /users/sign_out(.:format)     devise/sessions#destroy
user_password        POST    /users/password(.:format)     devise/passwords#create
new_user_password    GET     /users/password/new(.:format) devise/passwords#new
edit_user_password   GET     /users/password/edit(.:format)devise/passwords#edit
                     PUT     /users/password(.:format)     devise/passwords#update
cancel_user_registration GET /users/cancel(.:format)       registrations#cancel
user_registration    POST    /users(.:format)              registrations#create
new_user_registration GET    /users/sign_up(.:format)      registrations#new
edit_user_registration GET   /users/edit(.:format)         registrations#edit
                      PUT    /users(.:format)              registrations#update
                      DELETE /users(.:format)              registrations#destroy
admin_edit_user       GET    /admin/users/:id/edit(.:format) users/registrations#edit
users                 GET    /admin/users(.:format)        users#index
                      POST   /admin/users(.:format)        users#create
new_user              GET    /admin/users/new(.:format)    users#new
edit_user             GET    /admin/users/:id/edit(.:format) users#edit
user                  GET    /admin/users/:id(.:format)     users#show
                      PUT    /admin/users/:id(.:format)     users#update
                      DELETE /admin/users/:id(.:format)     users#destroy

用户视图(设计/注册/编辑等)已正确连接并且工作正常,但管理视图全部从用户文件夹(用户/编辑等)中提取,这意味着它们绕过了设计。

例如,我的管理员用户编辑表单在 form_for 中应该有一些变化:

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>

但该代码不会提取任何用户数据(所有表单字段均为空)。我可以让它显示的唯一方法是使用旧代码:

<%= form_for @user do |f| %>

然后使用来自我的 users_controller 的更新方法,而不是来自 Devise registrations_controller。

我在路线上尝试了很多变化,因为这似乎是关键,但我的尝试都没有奏效。帮忙?

【问题讨论】:

  • 错误是我的管理视图页面没有使用正确的控制器,我不知道如何修复它。
  • 到目前为止你有没有运气?

标签: ruby-on-rails ruby-on-rails-3 devise


【解决方案1】:

为了让您的 form_for 助手通过 Devise 正确路由您的请求,您需要将您的 admin/users 资源路由声明为 Devise 路由:

# config/routes.rb
devise_for :users, :controllers => { :registrations => :registrations }

scope "/admin" do
  devise_for :users, :controllers => { :registrations => :registrations }
end

您将继续保留设计功能,同时能够将admin 命名空间用于授权目的。

【讨论】:

  • 实际上,现在我遇到了另一个问题 - 我的标准路由在 admin 命名空间中不起作用。 admin/users/:id/edit 给出了一个错误,所以我将 match '/users/:id/edit', to: 'registrations#edit', as: 'admin_edit_user' 添加到管理命名空间。然后我在 RegistrationsController#edit 中得到一个 undefined method 'name' for nil:NilClass 错误,所以我在我的 registrations_controller 中添加了一个编辑方法来定义 @user,但这没有区别。
  • 好吧,没关系,我只是要回滚并摆脱设计。它造成的问题比解决的问题多。
  • 您遇到的问题与路线无关。如果这个答案有助于解决您最初的问题,我认为它有,您应该考虑接受它是正确的。
猜你喜欢
  • 2018-10-18
  • 2014-10-26
  • 2013-09-13
  • 2016-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多