【问题标题】:How do you remove the edit button in an active admin show如何删除活动管理员节目中的编辑按钮
【发布时间】:2017-06-23 14:03:18
【问题描述】:

我想删除任何记录的活动管理显示页面上的编辑按钮,但月份和年份等于当前月份和年份的记录除外。

我可以用

在索引中做到这一点
  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

我的 execution_summaryies.rb 包括

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

代码当前的方式是,编辑按钮显示在标题栏中(我相信默认情况下),并且旁边有一个打印按钮。 (来自上面显示的代码)。

如何在显示/查看页面上仅显示当前月份和年份的记录的编辑按钮?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 activeadmin


    【解决方案1】:

    首先从所有#show页面中删除编辑按钮:

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

    然后有条件地添加:

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

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

    【讨论】:

      【解决方案2】:

      在您的executive_summaryies.rb 顶部,您应该能够执行这样的操作,这将完全删除编辑按钮。

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

      现在根本没有显示编辑按钮,您可以有条件地将其添加回来:

      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
      

      显然,show_edit_button? 方法必须使用要用于确定按钮是否显示的逻辑来定义。另外,您可以使用resource 来引用当前对象。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多