Active ActiveAdmin elements based on data state

I want to hide the editing path if the editing object has a certain status.

How can i do this?

+7
source share
5 answers

I finally did it. I needed two things:

Direct access redirection and hiding buttons on the edit page.

For redirection, when the user tries to access the edit page directly, I use the before_filter file:

before_filter :some_method, :only => [:edit, :update] def some_method redirect_to action: :show if status == something end 

To hide the buttons, I do it like this:

 ActiveAdmin.register Model do config.clear_action_items! action_item :only => [:show] , :if => proc { instance.status == something } do link_to 'Edit', edit_model_path(instance) end end 
+7
source

If you are talking about hiding the edit link, which is displayed by default (along with view and delete links) in the index action, you can customize the index view as follows:

 ActiveAdmin.register Model do index do column :actions do |object| raw( %(#{link_to "View", [:admin, object]} #{link_to "Delete", [:admin, object], method: :delete} #{(link_to"Edit", [:edit, :admin, object]) if object.status? }) ) end end end 

Since the contents of the column will only be what is returned by the column block, you need to immediately return all three (or two) links as a string. Here, raw used so that actual links are displayed, not html for links.

+6
source

This can be achieved using the following:

 ActiveAdmin.register Object do index do column :name actions defaults: true do |object| link_to 'Archive', archive_admin_post_path(post) if object.status? end end end 

Note that using defaults: true will add your custom actions to the active default admin actions.

+1
source

You can create before_filter in your controller, which applies only to the edit action. It can check the status and allow it to run or redirect_to depending on the return method.

Something like this in your application controller:

 def some_method if foo.bar == true redirect_to foos_path end end 

Then at the beginning of your security question

 before_filter :some_method, :only => :edit 
0
source

If you want to hide the "edit" link (in active_admin views) for the object, if the object has a specific value, u can override the default view for the method and add a condition before the link is displayed.

-one
source

All Articles