Custom action and Activeadmin form

I want to implement a user action (notify_all) on my activeadmin Users page, which when clicked will display a form that will be redirected to another user action (send_notification_to_all) when submitted. So far, I have not been able to get the second part to work.

admin / 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

When you click the Notify All button, the following view is displayed. views / admin / users / 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>

When this form is submitted, I get 401 Unauthorized error:

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

Is it possible to do what I'm trying to do, although an active administrator?

+4
source share
3 answers

, .

, , :

<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>

.

+4

Rails, Formtastic ActiveAdmin , .

Formtastic semantic_form_for form builder:

<%= 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 ActiveAdmin.

+8

Arbre

form do |f|
  input type: :hidden, name: 'authenticity_token', value: form_authenticity_token.to_s
0

Source: https://habr.com/ru/post/1526617/


All Articles