简体   繁体   中英

How to add an action in active_admin in rails

I am working on active_admin in rails in which I want to add an action which will run a service.

action should appear in the index view. so, how can i add a action which runs this service.

My code of app/admin/website.rb is

ActiveAdmin.register Website do
  actions  :index

  index do
    selectable_column
    id_column
    column :state
    column :name
    column :created_at
    column :updated_at
  end
end

in above file i want to check that id state='draft' then it should run the action which runs a service.

My service file app/services/website_verify_state_service.rb:

class WebsiteVerifyStateService
    
  def self.process!(website)
    new(website).process
  end

  def initialize(website)
    @website = website
  end

  def process
    site_response = self.class.post("#{BASE_MONO_URL}reseller/account/site", 
      
    site_data = JSON.parse(site_response.body)['data']
    if site_data
      @website.update(published: true) if site_data[0]['site']['lastPublishDate'].present?
    end
  end
end

SO my only concern is that how i call this service through action when from active admin. Thanks in advance

You can add conditional actions to the index table:

ActiveAdmin.register Website do
  actions  :index

  index do
    selectable_column
    id_column
    column :state
    column :name
    column :created_at
    column :updated_at
    actions do |website|
      item('Verify state', verify_admin_websites_path(website.id), class: 'member_link') if website.state == 'draft'
    end
  end

  member_action :verify do
    # do your magic here, like or whatever you do
    WebsiteVerifyStateService.process!
    redirect_back(fallback_location: root_path)
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM