在 ActiveAdmin 中,发布到 collection_action 实际上是由 :create 成员操作参与的
In ActiveAdmin, posting to a collection_action is being attended, actually, by the :create member action
我有这个 ActiveAdmin 注册
ActiveAdmin.register User do
permit_params :first_name, :last_name, :email, :role
scope :all
scope :admin
scope :professor
scope :student
config.clear_action_items!
index do
id_column
column :name do |c|
c.full_name
end
column :email
column :created_at do |c|
c.created_at.strftime("%Y-%m-%d %H:%M")
end
column :role
column :login_as do |c|
link_to "login as", login_as_admin_user_path(c.id)
end
actions defaults: false do |c|
item 'Show', admin_user_path(c.id)
span ' | '
item 'Edit', edit_admin_user_path(c.id)
end
end
filter :first_name
filter :last_name
filter :email
form do |f|
f.inputs do
f.input :first_name
f.input :last_name
f.input :email
if f.object.new_record?
f.input :role, as: :select, collection: [['Professor', :professor], ['Student', :student]]
elsif f.object.admin?
f.input :role, as: :readonly, display: 'Administrator'
else
f.input :role, as: :select, collection: [['Professor', :professor], ['Student', :student], ['Administrator', :admin]]
end
end
f.actions
end
action_item :only => :index do
link_to 'Invite User', new_admin_user_path
end
action_item :only => :index do
link_to 'Bulk Invite Users', bulk_new_admin_users_path
end
collection_action :bulk_new, method: :get do
# Just render - no specific implementation
end
collection_action :bulk_create, method: :post do
# Also tried: raise StandardError.new 'foo'
render nothing: true
end
member_action :create, method: :post do
pparams = permitted_params[:user]
@user = User.new(pparams)
if @user.admin?
flash[:error] = 'Cannot invite admin users with this method'
render :new
else
@user.avoid_password_validation = true
if @user.valid?
@user.invite!(current_user)
redirect_to admin_user_path(@user.id), notice: "Invitation sent to email: #{@user.email}"
else
render :new
end
end
end
member_action :login_as do
new_user = User.find(params[:id])
session["admin_user_id"] = current_user.id
sign_in(:user, new_user)
redirect_to root_path
end
member_action :resend_activation do
@user = User.find(params[:id])
@user.send_confirmation_instructions
end
show do
attributes_table :id, :first_name, :last_name, :email, :role, :encrypted_password, :reset_password_oken, :reset_password_sent_at, :remember_created_at,
:sign_in_count, :current_sign_in_at, :last_sign_in_at, :current_sign_in_ip, :last_sign_in_ip, :invitation_token, :invitation_created_at,
:invitation_sent_at, :invitation_accepted_at, :invitation_limit, :invited_by, :invited_by_type, :invitations_count, :created_at,
:updated_at
end
end
我的路线是这样生成的:
admin_user POST /admin/users/:id(.:format) admin/users#create
login_as_admin_user GET /admin/users/:id/login_as(.:format) admin/users#login_as
resend_activation_admin_user GET /admin/users/:id/resend_activation(.:format) admin/users#resend_activation
bulk_new_admin_users GET /admin/users/bulk_new(.:format) admin/users#bulk_new
bulk_create_admin_users POST /admin/users/bulk_create(.:format) admin/users#bulk_create
batch_action_admin_users POST /admin/users/batch_action(.:format) admin/users#batch_action
admin_users GET /admin/users(.:format) admin/users#index
POST /admin/users(.:format) admin/users#create
new_admin_user GET /admin/users/new(.:format) admin/users#new
edit_admin_user GET /admin/users/:id/edit(.:format) admin/users#edit
GET /admin/users/:id(.:format) admin/users#show
PATCH /admin/users/:id(.:format) admin/users#update
PUT /admin/users/:id(.:format) admin/users#update
DELETE /admin/users/:id(.:format) admin/users#destroy
当我到达 /admin/users/bulk_new
路由时,它只是一个表单点击 /admin/users/bulk_create
(Url: bulk_create_admin_users_path
,我可以在浏览器本身验证它是否为真, 没有 url 包装或任何类似的干预),它按预期显示。
但是,当我提交时(同时检查浏览器的网络选项卡以检查是否存在重定向 - url 是 /admin/users/bulk_create
),post 请求由 :create
成员操作(由于未提供用户数据而失败)而不是由 :bulk_create
收集操作处理。
我在这里错过了什么
根据 rake routes
的输出,路由似乎有问题,其中第一个 admin_user POST /admin/users/:id(.:format) admin/users#create
与您要用作 bulk_create 的格式相同, rails 被视为 :id
bulk_create
我有这个 ActiveAdmin 注册
ActiveAdmin.register User do
permit_params :first_name, :last_name, :email, :role
scope :all
scope :admin
scope :professor
scope :student
config.clear_action_items!
index do
id_column
column :name do |c|
c.full_name
end
column :email
column :created_at do |c|
c.created_at.strftime("%Y-%m-%d %H:%M")
end
column :role
column :login_as do |c|
link_to "login as", login_as_admin_user_path(c.id)
end
actions defaults: false do |c|
item 'Show', admin_user_path(c.id)
span ' | '
item 'Edit', edit_admin_user_path(c.id)
end
end
filter :first_name
filter :last_name
filter :email
form do |f|
f.inputs do
f.input :first_name
f.input :last_name
f.input :email
if f.object.new_record?
f.input :role, as: :select, collection: [['Professor', :professor], ['Student', :student]]
elsif f.object.admin?
f.input :role, as: :readonly, display: 'Administrator'
else
f.input :role, as: :select, collection: [['Professor', :professor], ['Student', :student], ['Administrator', :admin]]
end
end
f.actions
end
action_item :only => :index do
link_to 'Invite User', new_admin_user_path
end
action_item :only => :index do
link_to 'Bulk Invite Users', bulk_new_admin_users_path
end
collection_action :bulk_new, method: :get do
# Just render - no specific implementation
end
collection_action :bulk_create, method: :post do
# Also tried: raise StandardError.new 'foo'
render nothing: true
end
member_action :create, method: :post do
pparams = permitted_params[:user]
@user = User.new(pparams)
if @user.admin?
flash[:error] = 'Cannot invite admin users with this method'
render :new
else
@user.avoid_password_validation = true
if @user.valid?
@user.invite!(current_user)
redirect_to admin_user_path(@user.id), notice: "Invitation sent to email: #{@user.email}"
else
render :new
end
end
end
member_action :login_as do
new_user = User.find(params[:id])
session["admin_user_id"] = current_user.id
sign_in(:user, new_user)
redirect_to root_path
end
member_action :resend_activation do
@user = User.find(params[:id])
@user.send_confirmation_instructions
end
show do
attributes_table :id, :first_name, :last_name, :email, :role, :encrypted_password, :reset_password_oken, :reset_password_sent_at, :remember_created_at,
:sign_in_count, :current_sign_in_at, :last_sign_in_at, :current_sign_in_ip, :last_sign_in_ip, :invitation_token, :invitation_created_at,
:invitation_sent_at, :invitation_accepted_at, :invitation_limit, :invited_by, :invited_by_type, :invitations_count, :created_at,
:updated_at
end
end
我的路线是这样生成的:
admin_user POST /admin/users/:id(.:format) admin/users#create
login_as_admin_user GET /admin/users/:id/login_as(.:format) admin/users#login_as
resend_activation_admin_user GET /admin/users/:id/resend_activation(.:format) admin/users#resend_activation
bulk_new_admin_users GET /admin/users/bulk_new(.:format) admin/users#bulk_new
bulk_create_admin_users POST /admin/users/bulk_create(.:format) admin/users#bulk_create
batch_action_admin_users POST /admin/users/batch_action(.:format) admin/users#batch_action
admin_users GET /admin/users(.:format) admin/users#index
POST /admin/users(.:format) admin/users#create
new_admin_user GET /admin/users/new(.:format) admin/users#new
edit_admin_user GET /admin/users/:id/edit(.:format) admin/users#edit
GET /admin/users/:id(.:format) admin/users#show
PATCH /admin/users/:id(.:format) admin/users#update
PUT /admin/users/:id(.:format) admin/users#update
DELETE /admin/users/:id(.:format) admin/users#destroy
当我到达 /admin/users/bulk_new
路由时,它只是一个表单点击 /admin/users/bulk_create
(Url: bulk_create_admin_users_path
,我可以在浏览器本身验证它是否为真, 没有 url 包装或任何类似的干预),它按预期显示。
但是,当我提交时(同时检查浏览器的网络选项卡以检查是否存在重定向 - url 是 /admin/users/bulk_create
),post 请求由 :create
成员操作(由于未提供用户数据而失败)而不是由 :bulk_create
收集操作处理。
我在这里错过了什么
根据 rake routes
的输出,路由似乎有问题,其中第一个 admin_user POST /admin/users/:id(.:format) admin/users#create
与您要用作 bulk_create 的格式相同, rails 被视为 :id
bulk_create