Code Monkey home page Code Monkey logo

Comments (14)

gregbell avatar gregbell commented on May 14, 2024 2

The best way is to use the "member_action" and "collection_action" methods. I am in the process of writing the docs for them right now. Until then, this should do:

ActiveAdmin.register Post do

  # Available at /admin/posts/1/approve and #approve_admin_post_path(post)
  member_action :approve, :method => :post do
    # Just a regular controller method in here
    post = Post.find params[:id]
    post.approve!
    redirect_to admin_post_path(post)
  end

  # The exact same api is available for collection actions
  # Available at /admin/posts/some_collection and #some_collection_admin_posts
  collection_action :some_collection

end

Checkout the methods in the DSL at https://github.com/gregbell/active_admin/blob/master/lib/active_admin/dsl.rb#L118

from activeadmin.

imarbol avatar imarbol commented on May 14, 2024 1

To get the layout in your view it's also nice having this:
render "name_of_your_template", :layout => "active_admin"
It's not valid with a partial, only with a template, as far as I know.

from activeadmin.

joaomilho avatar joaomilho commented on May 14, 2024

Woooooooo... worked perfectly. No need for routes. Awesome again.

from activeadmin.

bmorton avatar bmorton commented on May 14, 2024

Oh, nice! This is just what I was looking for :)

from activeadmin.

ajn avatar ajn commented on May 14, 2024

I've added a member action similar to the above example, however it doesn't include a redirect.
The action renders fine, except for it has no layout; has anyone had this problem? If so, how did you overcome it?

from activeadmin.

benvds avatar benvds commented on May 14, 2024

Hi, I also don't get the layout. Have you solved this issue?

from activeadmin.

ajn avatar ajn commented on May 14, 2024

Not solved, but hacked into existence with the following:

# app/config/initializers/active_admin.rb

require 'inherited_views'

module ActiveAdmin

  class ResourceController < ::InheritedViews::Base

    protected

    def renderer_for(action)
      if %w(index new create edit update destroy show).include?(action.to_s)
        ActiveAdmin.view_factory["#{action}_page"]
      else
        ActiveAdmin.view_factory["abstract_page"]
      end
    end

  end

  module Views
    module Pages
      class AbstractAction < Base

        def title
          params[:action].to_s.titleize
        end

        def main_content
          render(params[:action])
        end

      end
    end
  end

  class ViewFactory < AbstractViewFactory
    register :abstract_page => ActiveAdmin::Views::Pages::AbstractAction
  end

end

If, for example, you had a member action called preview on a Post resource, you would create a corresponding action template with the html.arb extension:

# app/views/admin/posts/preview.html.arb
render renderer_for(:preview)

This will render the AbstractPage view factory which will, in turn, render a preview partial.
You can then create this partial (erb, arb, haml, whatever) and fill it with the approriate content.

This is by no means a sustainable way to do things, but it seems to work.

from activeadmin.

Zequez avatar Zequez commented on May 14, 2024

It would be nice if member_action also added an action to the default_actions in the index :P

from activeadmin.

rgalindo33 avatar rgalindo33 commented on May 14, 2024

i agree with zequez

from activeadmin.

IslamAzab avatar IslamAzab commented on May 14, 2024

I have something like this

   member_action :blow, :method => :post do
      puts "$"*30
      puts "Doing something!"
      puts "$"*30
      redirect_to admin_cluster_path, notice: "Did you notice that?!"
    end

However the route is not added of the resource and I get the following error:

Routing Error
No route matches [GET] "/backend/admin/clusters/12345/blow"

from activeadmin.

shekibobo avatar shekibobo commented on May 14, 2024

@IslamAzab There is no route for the get verb on that action. There is a route for post on that action. If you're using a link, I might suggest using :method => :put and making sure you add the :method => :put option on your link_to.

from activeadmin.

IslamAzab avatar IslamAzab commented on May 14, 2024

@shekibobo Oh, my bad. What you've suggested will work just fine. Thanks!

from activeadmin.

conficker1805 avatar conficker1805 commented on May 14, 2024

thank you @gregbell

from activeadmin.

rkt2spc avatar rkt2spc commented on May 14, 2024

How to authorize member_action and collection_action?

from activeadmin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.