0

我正在使用 rails administrate 开发一个 rails 应用程序。我有一个管理界面,其中包含一个admin_users由 devise 控制的资源。目前,用户实际上只在 Rails 控制台中创建,并且添加了设计以允许用户具有全身份验证功能。在该仪表板上,我想要一个选项来为我想邀请到应用程序中的其他用户创建 admin_users,因为这比在生产中从 rails 控制台执行此操作更容易。

我有一个控制器Admin::AdminUsersController来处理此功能以匹配仪表板。现在,它列出了系统中从 rails 控制台创建的当前 admin_users 并且工作正常。

现在,我添加了从 rails 管理界面创建管理员用户的选项。但是,在填写表格时admin/admin_users/new,devise 似乎拦截了请求,并且似乎试图让我退出。查看我的 Rails 服务器,这里是日志

app_1  | Started POST "/admin/admin_users" for 192.168.96.1 at 2020-02-25 02:38:41 +0000
app_1  | Cannot render console from 192.168.96.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1
app_1  | Processing by Devise::RegistrationsController#create as HTML
app_1  |   Parameters: {"authenticity_token"=>"LPbT7OCx8upC5ehWIZ1JV/o/UKttciyoThOgi06B4AUw8ve3x/stbjUT7Trvb9u0YMEoyEBobbbni0g7LNWvBg==", "admin_user"=>{"email"=>"test@somemail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "reset_password_token"=>"[FILTERED]", "reset_password_sent_at"=>"[FILTERED]", "remember_created_at"=>""}, "commit"=>"Create Admin user"}
app_1  |   AdminUser Load (0.6ms)  SELECT "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = $1 ORDER BY "admin_users"."id" ASC LIMIT $2  [["id", "2d795b9d-2b6f-4745-a58d-775076100c60"], ["LIMIT", 1]]
app_1  | Redirected to http://localhost:3000/
app_1  | Filter chain halted as :require_no_authentication rendered or redirected
app_1  | Completed 302 Found in 6ms (ActiveRecord: 0.6ms | Allocations: 2088)
app_1  | 
app_1  | 
app_1  | Started GET "/" for 192.168.96.1 at 2020-02-25 02:38:41 +0000
app_1  | Cannot render console from 192.168.96.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1
app_1  | Processing by Rails::WelcomeController#index as HTML
app_1  |   Rendering /usr/local/bundle/gems/railties-6.0.2.1/lib/rails/templates/rails/welcome/index.html.erb
app_1  |   Rendered /usr/local/bundle/gems/railties-6.0.2.1/lib/rails/templates/rails/welcome/index.html.erb (Duration: 3.6ms | Allocations: 201)
app_1  | Completed 200 OK in 6ms (Views: 5.4ms | ActiveRecord: 0.0ms | Allocations: 1004)
app_1  | 

而且我无法让控制器执行我的Admin::AdminUsersController#create操作。查看日志,它看起来像是在 到达我的发布路线,在/admin/admin_users设计拦截它Devise::RegistrationsController#create,从 停止Filter chain halted as :require_no_authentication,然后重定向到我的根路线,这与我注销时类似。

我什至添加了一个视图来覆盖管理员的默认视图,app/views/admin/admin_users/new.html.erb以触发表单转到我正在寻找的控制器操作,就像这样

<section class="main-content__body">
  <%= form_for([namespace, page.resource], :url => url_for(:controller => 'admin/admin_users', :action => 'create'), html: { class: "form" }) do |f| %>
    <% if page.resource.errors.any? %>
      <div id="error_explanation">
        <h2>
          <%= pluralize(page.resource.errors.count, "error") %>
          prohibited this <%= page.resource_name %> from being saved:
        </h2>

        <ul>
          <% page.resource.errors.full_messages.each do |message| %>
            <li class="flash-error"><%= message %></li>
          <% end %>
        </ul>
      </div>
    <% end %>

    <% page.attributes.each do |attribute| -%>
      <div class="field-unit field-unit--<%= attribute.html_class %>">
        <%= render_field attribute, f: f %>
      </div>
    <% end -%>

    <div class="form-actions">
      <%= f.submit %>
    </div>
  <% end %>

</section>

我也尝试devise_scope :admin_user在我的路线中添加我的

devise_scope :admin_user do
  post "/admin/admin_users", to: 'admin/admin_users#create'
end

这没有做任何事情。

我正在寻找的功能是当我在我的 admin_users 仪表板上时,使用我的Admin::AdminUsersController#create操作而不是Devise::RegistrationsController#create操作创建一个 admin_user。

有谁知道我需要添加什么来启用它?

4

1 回答 1

0

您是否尝试过命名空间?

namespace :admin do 
  devise_for :users
  # if the above doesn't work 
  # devise_for :admin_users
end

此外,您可以尝试覆盖Administrate此处描述的基本控制器:https ://stackoverflow.com/a/59824297/1737811以及必要的字段。

于 2020-02-25T08:21:37.057 回答