gpt4 book ai didi

ruby-on-rails - Activeadmin 自定义操作和表单

转载 作者:行者123 更新时间:2023-12-04 22:05:05 25 4
gpt4 key购买 nike

我想在我的用户 activeadmin 页面上实现一个自定义操作 (notify_all),单击该操作将显示一个表单,提交时将路由到另一个自定义操作 (send_notification_to_all)。到目前为止,我一直无法让第二部分工作。

管理员/用户.rb:

ActiveAdmin.register User do

action_item :only => :index do
link_to 'Notify All', notify_all_admin_users_path
end

collection_action :notify_all, :method => :get do
puts "notifying...."
end

collection_action :send_notification_to_all, :method => :post do
puts "sending notification...."
end



end

单击 Notify All 按钮时,将呈现以下 View 。
意见/管理员/用户/notify_all.html.erb
<form action="send_notification_to_all" method="post">
<div><textarea rows="10" cols="100" placeholder="Enter message here"></textarea></div>
<div><input type="submit"></div>
</form>

提交此表单时,我收到 401 Unauthorized 错误:
Started POST "/admin/users/send_notification_to_all" for 127.0.0.1 at 2014-02-12 14:08:27 -0600
Processing by Admin::UsersController#send_notification_to_all as HTML
WARNING: Can't verify CSRF token authenticity
AdminUser Load (0.8ms) SELECT "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = 1 LIMIT 1
(0.3ms) BEGIN
(26.6ms) UPDATE "admin_users" SET "remember_created_at" = NULL, "updated_at" = '2014-02-12 14:08:27.394791' WHERE "admin_users"."id" = 1
(20.3ms) COMMIT
Completed 401 Unauthorized in 108.3ms

是否可以通过活跃的管理员来做我想做的事情?

最佳答案

使用 Rails、Formtastic 或 ActiveAdmin 表单构建器可以完全避免这个问题,因为它会自动为您呈现真实性 token 。

使用 Formtastic 的 semantic_form_for 重写您的表单表单生成器:

<%= semantic_form_for :notification, url: { action: :send_notification } do |f| %>

<%= f.inputs do %>
<%= f.input :content, as: :text, input_html: { placeholder: "Enter message here" } %>
<%- end %>

<%= f.actions %>
<%- end %>

可能值得一读 Formtastic 的 documentation更多细节。默认情况下,ActiveAdmin 包含 Formtastic。

关于ruby-on-rails - Activeadmin 自定义操作和表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21738947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com