sep*_*err 4 ruby-on-rails activeadmin
我想在我的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
Run Code Online (Sandbox Code Playgroud)
单击"通知全部"按钮时,将呈现以下视图.意见/管理/用户/ 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>
Run Code Online (Sandbox Code Playgroud)
提交此表单后,我收到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
Run Code Online (Sandbox Code Playgroud)
虽然主动管理员可以做我想做的事吗?
使用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 %>
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,可能需要阅读Formtastic的文档.默认情况下,Formtastic包含在ActiveAdmin中.
在此处提出的类似问题中找到了答案。
我修改了表单以包含身份验证令牌,如下所示:
<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>
Run Code Online (Sandbox Code Playgroud)
这解决了这个问题。
| 归档时间: |
|
| 查看次数: |
9455 次 |
| 最近记录: |