我想在我的Users activeadmin页面上实现一个自定义操作(notify_all),当单击该页面时,将显示一个表单,该表单在提交时将路由到另一个自定义操作(send_notification_to_all)。到目前为止,我无法让第二部分工作。
管理员/ users.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.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
虽然有效的管理员可以做我想做的事吗?
答案 0 :(得分:7)
使用Rails,Formtastic或ActiveAdmin表单构建器可以完全避免这个问题,因为它会自动为您呈现真实性令牌。
使用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阅读。默认情况下,Formtastic包含在ActiveAdmin中。
答案 1 :(得分:4)
在类似问题中找到答案here。
我修改了表单以包含身份验证令牌,如下所示:
<form action="send_notification_to_all" method="post">
<input type="hidden" name="authenticity_token" value="#{form_authenticity_token.to_s}">
<div><textarea rows="10" cols="100" placeholder="Enter message here"></textarea></div>
<div><input type="submit"></div>
</form>
这解决了这个问题。
答案 2 :(得分:0)
使用Arbre,您可以编写
form do |f|
input type: :hidden, name: 'authenticity_token', value: form_authenticity_token.to_s