Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activeadmin Custom Action And Form

I want to implement a custom action (notify_all) on my Users activeadmin page that when clicked will display a form which when submitted will route to another custom action (send_notification_to_all). So far I have been unable to get the second part working.

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 Notify All button is clicked, following view is rendered. 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 a 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 am trying to do though active admin?

like image 703
septerr Avatar asked Feb 12 '14 20:02

septerr


3 Answers

Using Rails, Formtastic, or ActiveAdmin form builders would avoid the issue altogether as it would automatically render the authenticity token for you.

Rewriting your form using Formtastic's 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 %>

It may be worth reading through Formtastic's documentation for more details. Formtastic is included with ActiveAdmin by default.

like image 126
Charles Maresh Avatar answered Sep 22 '22 20:09

Charles Maresh


Found the answer in a similar question asked here.

I modified my form to include the authentication token as follows:

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

This resolved the issue.

like image 38
septerr Avatar answered Sep 22 '22 20:09

septerr


Using Arbre you can write

form do |f|
  input type: :hidden, name: 'authenticity_token', value: form_authenticity_token.to_s
like image 25
Salz Avatar answered Sep 26 '22 20:09

Salz