Chris Mendla
Chris Mendla

Reputation: 1017

How do you remove the edit button in an active admin show

I want to remove the edit button on active admin show pages for any record except for those where the month and year equal the current month and year.

I was able to do that in the index with

  if (es.month == Date.today.strftime("%m").to_i) && (es.year == Date.today.strftime("%Y").to_i )
    span link_to 'Edit', "/executive_summaries/#{es.id}/edit"
  end

My executive_summaryies.rb includes

ActiveAdmin.register Executive::Summary do
  menu :parent => 'Reports'

  config.batch_actions = true

 . . .

  action_item :only => :show do
     link_to_function("#{ActiveAdmin::Iconic.icon(:document_stroke)} Print Executive Summary".html_safe, "javascript:print()")

  end

 . . .

 controller do

 . . . 

    def edit
      @run_js_functions = %w(ExecutiveSummaries.init)
    end

    def show
      @run_js_functions = %w(ExecutiveSummaries.accordion)
    end

    . . . 

end

The way the code is currently, the edit button shows in the titlebar (by default I believe) and there is a print button next to it. (From the code in the show above).

How can I just show the edit button for records that are for the current month and year on the show/view page?

Upvotes: 2

Views: 4606

Answers (2)

Anthony Wang
Anthony Wang

Reputation: 1315

First remove the edit button from all #show pages:

config.action_items.delete_if {|item| item.name == :edit && item.display_on?(:show) }

Then add it conditionally:

action_item :my_action, only: :show, if: proc { resource.year = Date.today.year && resource.month == Date.today.month }
  link_to ...
end

h/t https://hungnhotblog.wordpress.com/2016/09/14/conditionally-show-buttons-on-show-page/

Upvotes: 7

treiff
treiff

Reputation: 1306

At the top of your executive_summaryies.rb, you should be able to do something like this which will remove the edit button totally.

ActiveAdmin.register Executive::Summary do
  actions :all, except: [:edit]
  ...
end

Now that the edit button is not shown at all, you could then add it back conditionally:

ActiveAdmin.register Executive::Summary do
  actions :all, except: [:edit]
  ...

  action_item only: :show do
    if show_edit_button?
      link_to 'Edit', '/executive_summaries/#{resource.id}/edit'
    end
  end
end

Obviously, the show_edit_button? method you'd have to define with the logic you want to use to determine is the button is displayed or not. Also, you can use resource to refer to the current object.

Upvotes: 0

Related Questions