加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 03:55:50 所属栏目:百科 来源:网络整理
导读:我想在我的Users activeadmin页面上实现一个自定义操作(notify_all),单击该页面时将显示一个表单,该表单在提交时将路由到另一个自定义操作(send_notification_to_all).到目前为止,我一直无法使第二部分工作. 管理员/ users.rb的: ActiveAdmin.register User
我想在我的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

虽然主动管理员可以做我想做的事吗?

解决方法

使用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中.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读